• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Socket Close Detection, Late FINs

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic