有问题的代码流程:
ServerSocket serverSocket = new ServerSocket(8000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//接收输入流操作
is.close();
OutputStream os = socket.getOutputStream();
//输出返回值os.write();
os.close();
socket.close();
没问题的流程:
ServerSocket serverSocket = new ServerSocket(8000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
//接收输入流操作
OutputStream os = socket.getOutputStream();
//输出返回值os.write();
is.close();
os.close();
socket.close();
就这样的看,我真看不出个所以然,但是确实前者能完成返回消息,后者可以返回,求明白!
socket? getInputStream getOutputStream close
------解决方案--------------------
ServerSocket serverSocket = new ServerSocket(8000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
is.close();
System.out.println(socket.isClosed());
其实你关闭stream的时候,socket也关闭了。
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.
大概设计者的意思是说,这是个全双工的通讯,Server端的职责是响应请求,所以一个流关闭,则socket也没办法工作了