我需要知道以下给定程序的流程:
客户类:
1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();
服务器类:
1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();
当我运行服务器类然后运行客户端类时,程序的流程如何工作?
答案 0 :(得分:2)
它完全按照它的读取方式流动。
服务器将套接字绑定到端口1101,侦听连接
1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
客户端连接到服务器并发送“hello”并关闭连接。
1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();
服务器在建立连接后从输入流中读取并打印行,然后关闭阅读器。
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();