This is a wierd problem I have been facing.
I understand that the only way that you can detect a remote client socket close(graceful only, NOT reset) in
java is through reading (or writing )on a socket and detect IOException. For the final FIN/ACK the server should close the socket in the Exception handling.
I have a server that currently does this in two ways. I read on the socket for a message on the socket.
1. Using a DataInputStream (reads 2Bytes)
2. Using a BufferedReader (reads a line)
I have a client that opens n connections and closes them after 20 sec. I am doing 'netstat -al' to grep on the port to determine the status of connections on the server.
Problem 1.
When I am using BufferedReader, and open 100 connections to the server and close them (after 20 sec), I see that all the connections are closed in a matter of ms. (netstat -al| grep <portnum> shows no connections in CLOSE_WAIT). But on the other hand when I use DataInputStream some connections are lingering in CLOSE_WAIT for more than 20 seconds (this time increases with the number of connections).
Why is this?
Problem 2.
The DataInputStream.read(2bytearray) does not throw IOException when I close the socket on the client side. It blocks till I close the connection on the client side. When the connection is closed the read succeeds. This to me is really wierd. Is this not a bug?
I tried DataInputStream.readFully(2bytearray) and this works. This throws an Exception when the Client socket is closed.
Any light on these issues is extremely appreciated.
Thanks
Subu