java.net.SocketException: socket closed

Open source software supply chain lighting plan, waiting for you>>>

When using socket programming, it is found that when the getinputstream method is used, the OutputStream will be returned

java.net.SocketE xception:socketclosed Wrong

See the Java API

Java.net.socket.getinputstream method:

getInputStream

public InputStream getInputStream()throws IOException

Returns the input stream for this socket

If the socket has an associated channel, the resulting input stream delegates all its operations to the channel. If the channel is in non blocking mode, the read operation of the input stream will throw an illegalblockingmodeexception

Under abnormal conditions, the underlying connection may be interrupted by the remote host or network software (for example, connection reset in the case of TCP connection). When the network software detects an interrupted connection, the following operations will be applied to the returned input stream:

Network software may discard the bytes buffered by the socket. The bytes not discarded by network software can be read by read

If no bytes are buffered on the socket, or read has consumed all the buffered bytes, all subsequent calls to read will throw an IOException

If no bytes are buffered on the socket and the socket is not closed with close, available returns 0

Closing the returned InputStream will close the associated socket

Return:

The input stream that reads bytes from this socket

Throw out:

IOException – if an I/O error occurs while creating an input stream, if the socket is not closed, if the socket is not connected, or if the socket input is closed by using shutdown input()

But

I found that InputStream was not shut down

Finally, it was found that the wrapper object of InputStream was closed, so the conclusion is that you should not try to close any stream when you want to transfer data

More articles: http://blog.gavinzh.com

Similar Posts: