socket连接两台电脑通信java

网上看的,测试成功,写下来



服务器端

import java.net.*;
import java.io.*;
 
public class Server {
 
    private ServerSocket server;
    private Socket client;
 
    public Server() {
        boolean flag = true;
        try {
            server = new ServerSocket(8888);    //端口
            System.out.println("服务器正在等到客户端连接......");
            while (flag) {
                client = server.accept();   //阻塞
                new ServerThread(client).start();
            }
            server.close();
        } catch (Exception e) {
        }
    }
 
    public static void main(String[] args) {
        new Server();
    }
 
    private class ServerThread extends Thread {
 
        private Socket clientThread;
        private PrintStream out;
        private BufferedReader in;
        private boolean flag = true;
 
        public ServerThread(Socket client) {
            this.clientThread = client;
            System.out.println("主机与客户端成功建立连接,开始通讯!!");
        }
 
        public void run() {
            try {
                /*
                 * 超时判断:设置超时时间300秒,中断连接或者300秒内客户端无回应信息则认为中断
                 * 缺点便是必须把超时时间设置很长,否则客户端待机也认为超时处理,无法判断是否端口
                 */
                clientThread.setSoTimeout(300000);
                out = new PrintStream(client.getOutputStream());
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                while (flag) {
                    String str = in.readLine(); //阻塞
                    System.out.println("客户端:" + str);
                    out.println("主机成功接收到您的信息!!");
                }
                client.close();
            } catch (Exception e) {
                //如果异常是连接异常,则输出连接断开
                if (e.getMessage() == "Connection reset") {
                    System.out.println("客户端已断开连接!!");
                }
            }
        }
    }
}



客户端


import java.net.*;
import java.io.*;
 
public class Client {
 
    private Socket client;
 
    public Client() {
        try {
            System.out.println("正在连接服务器......");
 
            client = new Socket("192.168.218.168", 8888);    //阻塞
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintStream out = new PrintStream(client.getOutputStream());;
            boolean flag = true;
 
            System.out.println("成功连接到主机,开始通讯!!");
 
            while (flag) {
                System.out.printf("请输入信息:");
                out.println(input.readLine());  //阻塞
                //判断是否与主机断开
                if (isConnected()) {
                    System.out.println("发生成功!!");
                    System.out.println("主机:" + in.readLine());
                } else {
                    System.out.println("发生失败!!");
                    System.out.println("与服务器断开连接!!");
                    client.close();
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
 
    //判断服务器是否断开的方法,
    //通过OutputStream发送一段测试数据,如果发送失败就表示远端已经断开连接
    //但会与正常的传送干扰,所以用sendUrgenData
    public boolean isConnected() {
        try {
            client.sendUrgentData(0xFF);
            return true;
        } catch (Exception e) {
            e.printStackTrack();
            return false;
        }
    }
 
    public static void main(String[] args) {
        new Client();
    }
}


你可能感兴趣的:(socket)