Socket InputStream.available may return a positive value after shutdown

Jing LV lvjing at linux.vnet.ibm.com
Tue Jan 25 07:32:48 PST 2011


Hello,

I am using java.net.Socket and find an strange behaviour. If Socket
inputstream is shutdown, and there was still some data remaining, its
available() method may still return a positive value. I checked with the
java spec, and the document of Socket.shutdownInput() said:
"Places the input stream for this socket at "end of stream". Any data
sent to the input stream side of the socket is acknowledged and then
silently discarded.
If you read from a socket input stream after invoking shutdownInput() on
the socket, the stream will return EOF. "
What's more, as I check inputStream.read(), it return a EOF, So I guess
it would not be correct if available return a positive value.
A simple testcase can be:

ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
Socket client = new Socket(InetAddress.getLocalHost(), port);

Socket worker = server.accept();
worker.setTcpNoDelay(true);

InputStream theInput = client.getInputStream();
OutputStream theOutput = worker.getOutputStream();

// send the regular data
String sendString = new String("Test");
theOutput.write(sendString.getBytes());
theOutput.flush();

// shutdown the input
client.shutdownInput();

assertEquals(-1, theInput.read());
// fails here. It is a bug not to return 0 to indicate EOF
assertEquals(0, theInput.available());

client.close();
server.close();

Any comments?




More information about the net-dev mailing list