2013年9月6日星期五

Trying to figure out , socket inside the input stream is closed , but the socket is not closed , why get into the output stream is not working ?

Problematic code process:

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();


No problem process:

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();


So look , I really do not see what they were , but it does the former can complete the return message , which can be returned , seeking to understand !
------ Solution ---------------------------------------- ----
		ServerSocket serverSocket = new ServerSocket(8000);
 
Socket socket = serverSocket.accept();
 
InputStream is = socket.getInputStream();
is.close();
System.out.println(socket.isClosed());

In fact, when you close the stream , socket has been closed .
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.
Probably designers mean , this is a full-duplex communication , Server -side is responsible for responding to the request , so a stream is closed , then the socket is no way to work
------ For reference only ---------------------------------------

------ For reference only ------ ---------------------------------
maybe you just look at an example , should be nothing problem ah ? Is not there no Tieshanglai client Socket client problem ?
------ For reference only -------------------------------------- -


client empathy :

this block and a corresponding block is started with a pair , and then go wrong.

Socket socket=new Socket(地址,端口)

OutputStream os = socket.getOutputStream();
//输出返回值os.write();
os.close();

InputStream is = socket.getInputStream();
//接收输入流操作
is.close();

socket.close();


then switch now , Normal:

Socket socket=new Socket(地址,端口)

OutputStream os = socket.getOutputStream();
//输出返回值os.write();
 
InputStream is = socket.getInputStream();
//接收输入流操作
 
is.close();
os.close();
socket.close();



own feeling seems to close an input and output streams , then socket does not work properly , and looked into the source code , but did not see what they were related, and seek to understand

began to write very simple when considering

ServerSocket server does not shut , accept obstruction to , there have been news , socket begin processing

output a client first , so they took a server , so the service first input stream , then finished as long as a return to the client , this segment is over, so I find it completely receiving input stream can be closed , and then to the output stream, and then off, and then close the socket .

corresponding client is the first to open the output stream , finished off, and then take the corresponding input stream , then finished off, and then close the socket

but not so dry practice to not understand why.
------ For reference only -------------------------------------- -
not deeply studied socket, looked under its constructor method , there are still bound to what, stream , socket as a whole, and thus the client and service side Socket Socket between affected. Server or client convection close operation will not affect their right to take another one stream operation , but it will affect another section ( client or server ) convection to take action.

these streams really annoying, and have this visible unity in place finally closed, how important it is , huh.
------ For reference only -------------------------------------- -


Thank you, a full-duplex communication is enough that he can not see the lack of capacity to understand socket, but the previous sentence plus network foundation to understand !

没有评论:

发表评论