服务器端:在服务器端,创建一个ServerSocket对象,并指定一个端号,使用ServerSocket类的accept()方法使服务器处于阻塞状态,等待用户请求。

接收:

  创建TCP服务端的Socket对象

  监听客户端对象

  获取输入流,读取数据

  释放资源

客户端:通过指定一个InetAddress对象和一个端口号,创建一个Socket对象,通过这个对象,连接到服务器。

  发送:

     创建TCP客户端的Socket对象

     获取输出流,写数据

     释放资源

20180619171453657.png

代码如下:

服务端


public class Server {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            ServerSocket ss = new ServerSocket(8888);
            //监听客户端的连接,socket就代表和客户端的连接
            Socket socket = ss.accept();
            new MessageThread(socket).start();
            //获取客户端的ip地址对象
            //打开输出流,向客户端输出信息
            dos = new DataOutputStream(socket.getOutputStream());
            System.out.println("请输入您想说的话:");
            String s = sc.nextLine();
            while(!s.equals("bye")) {
                dos.writeUTF(s);
                System.out.println("请输入您想说的话:");
                s = sc.nextLine();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private static class MessageThread extends Thread {
        Socket socket;
        public MessageThread(Socket socket) {
            this.socket = socket;
        }
        public void run() {
            DataInputStream dis = null;
            try {
                dis = new DataInputStream(socket.getInputStream());
                String s = dis.readUTF();
                while(true) {
                    System.out.println("client:" + s);
                    if(s.equals("bye")) {
                        break;
                    }
                    s = dis.readUTF();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                try {
                    dis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}


客户端:


public class Client {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        DataOutputStream dos = null;
        //指定Ip地址和端口,和服务器建立连接
        try {
            Socket socket = new Socket("127.0.0.1", 8888);
            new MessageThread(socket).start();
            //打开输出流,向服务端输出信息
            dos = new DataOutputStream(socket.getOutputStream());
            System.out.println("请输入您想说的话:");
            String s = sc.nextLine();
            while(!s.equals("bye")) {
                dos.writeUTF(s);
                System.out.println("请输入您想说的话:");
                s = sc.nextLine();
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private static class MessageThread extends Thread {
        Socket socket;
        public MessageThread(Socket socket) {
            this.socket = socket;
        }
        public void run() {
            DataInputStream dis = null;
            try {
                dis = new DataInputStream(socket.getInputStream());
                String s = dis.readUTF();
                while(true) {
                    System.out.println("server:" + s);
                    if(s.equals("bye")) {
                        break;
                    }
                    s = dis.readUTF();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                try {
                    dis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}



通过使用线程实现了双向通信。


原文链接:https://blog.csdn.net/qq_42383970/article/details/80736377