Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

ServerSockets - When Is It Closed/Not Connected?

Thread view: 
Hal Vaughan - 28 Mar 2006 03:25 GMT
I have a ServerSocket that creates a Socket with ServerSocket.accept().  I
then read data from that Socket's InputStream.  I'm sending data to it with
netcat.  When I stop netcat, there is no indication that I can see that the
connection is gone and netcat's client socket is gone.  I've checked with
Socket.isClosed(), isConnected(), isOutputShutdown(), and
isInputShutdown().  There is no indication that this Socket is no longer
communicating with netcat, which isn't running.

How can I detect this change and shut the Socket when/if its counterpart
shuts down?

Thanks for any help!

Hal
EJP - 28 Mar 2006 04:04 GMT
> How can I detect this change and shut the Socket when/if its counterpart
> shuts down?

You will read an EOF from the socket's input stream. At this point,
close the stream, or the socket.

NB see the javadoc for Socket.isClosed(), isConnected(),
isOutputShutdown(), and isInputShutdown(). They are not intended to tell
you about the state of the other end of the connection.
Missaka Wijekoon - 28 Mar 2006 07:33 GMT
> I have a ServerSocket that creates a Socket with ServerSocket.accept().  I
> then read data from that Socket's InputStream.  I'm sending data to it with
[quoted text clipped - 6 lines]
> How can I detect this change and shut the Socket when/if its counterpart
> shuts down?

You cannot assume or know the state of the socket on the other end.
What if the router in between was unplugged indefinitely?  So, you will
have to build into the protocol timeouts or some such mechanism to infer
that the other side has gone away.
mike - 28 Mar 2006 16:11 GMT
Set the socket timeout.

Something like this:
try {
  socket.setSoTimout(60000);
}
catch (SocketException e) {
  socket.close();
  return;
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.