• Socket实现客户端和服务端


    客户端

    public class Client {
        public static void main(String[] args) {
            // 服务端地址和端口
            final String DEFAULT_SERVER_HOST = "127.0.0.1";
            final Integer DEFAULT_SERVER_PORT = 8888;
            Socket socket = null;
            try {
                // 创建socket
                socket = new Socket(DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT);
                // 接收消息
                BufferedReader buffReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 发送消息
                BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                String msg = null;
                // 获取用户输入的消息
                BufferedReader userReader = new BufferedReader(new InputStreamReader(System.in));
                while (true) {
                    String input = userReader.readLine();
                    // 写入客户端要发送的消息。因为服务端用readLine获取消息,其以\n为终点,所以要在消息最后加上\n
                    buffWriter.write(input + "\n");
                    // 刷新缓存
                    buffWriter.flush();
                    // 读取服务端信息
                    msg = buffReader.readLine();
                    System.out.println(msg);
                    // 如果客户端输入quit就可以跳出循环、断开连接了
                    if (input.equals("quit")) {
                        break;
                    }
                }
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    服务端

    public class Server {
        public static void main(String[] args) {
            // 监听8888端口
            final int DEFAULT_PORT = 8888;
            ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(DEFAULT_PORT);
                while (true) {
                    // 阻塞监听
                    Socket socket = serverSocket.accept();
                    System.out.println("Client[" + socket.getPort() + "]Online");
                    // 读取消息
                    BufferedReader buffReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    // 发送消息
                    BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    String msg = null;
                    while ((msg = buffReader.readLine()) != null) {
                        System.out.println("Client[" + socket.getPort() + "]:" + msg);
                        // 写入服务端要发送的消息
                        buffWriter.write("Server:" + msg + "\n");
                        buffWriter.flush();
                        if (msg.equals("quit")) {
                            System.out.println("Client[" + socket.getPort() + "]:Offline");
                            break;
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    python之函数嵌套与闭包
    python之高阶函数
    python之装饰器
    python之内置函数
    python之内置函数:map ,filter ,reduce总结
    Python之reduce函数
    install python2 python3 in same computer
    git basic
    git LF CRLF
    2 thread, first to open chat window, second to make the phone
  • 原文地址:https://www.cnblogs.com/fangts/p/16284438.html
Copyright © 2020-2023  润新知