Solved

so_reuseaddr for server  sockets

Posted on 1997-04-24
7
277 Views
Last Modified: 2008-03-03
Is there any way to set the so_reuseaddr socket option when creating a java ServerSocket object?  

I'm using Sun jdk 1.1.  On NT, the ServerSocket is left open if my process is killed or crashes.  On Unix, the VM seems to cleanup correctly.
0
Comment
Question by:jmeier
7 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219817
I'm not sure about the so_reuseaddr in 1.1 but from what I have seen of socket support in Java, it's very limited. I'll look it up and let you know.

In the meantime, have you tried to close the socket manualy in the finalize() method?

Java guarantees (sort of) that the VM will call this method whenever it unloads from memory (I.e SIGQUIT,  SIGKILL and other critical signals or their equivalents uner other plattforms)
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219818
OK, the way to set the ro_reuseadr option is via the SocketImplFactory interface and then calling socket.setFactory().

I have not been able to find any actual examples (which is prompting me to do a book search in order to fill a gap in my education)  but there is a minimalistic explanation at:

http://java.cs.tamu.edu/jdk-1.1/docs/guide/net/extendingSocks.html#ServerSocket
0
 

Expert Comment

by:eeibmp1
ID: 1219819
I had a similar problem on NT.  I believe it is NT JVM bug. SO_REUSEADDR option really won't help here. All this option does is enables to reuse previously used address, for example if it is in one of these timing-out states (TIME_WAIT, FIN_WAIT2, etc).  If you kill a server on NT (^C for instance), you can then restart it right back on the same port. On UNIX with BSD sockets, if you do not specify SO_REUSEADDR, you'll get a bind error before the port times out completely. Apparently in Java it is taken care of automatically. However this problem that you described does have one very negative impact -- if you kill the server, and try to connect to its port, you will not only succeed, but the first block of data will go through like as if nothing is wrong. Only the second block of data will get the 'connection reset by peer' error. I already wrote to net@javasoft.com to report this bug and get it fixed. Hope this helps.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:jmeier
ID: 1219820
The SO_REUSEADDR option is exactly what I need.

The C++ version of the same test works correctly with the option.  Without it, it fails with a cannot bind address option.  The java version fails with the same error.

0
 

Author Comment

by:jmeier
ID: 1219821
socket.setFactory doesn't help me because it would mean writing the entire socket class myself.  What I'd like to do is override the one method in PlainSocket to set the option but the native setSocketOption method ignores SO_REUSEADDR.

Yes, I could rewrite the whole socket class myself but then I'd have native methods and I wouldn't be portable.  
0
 

Author Comment

by:jmeier
ID: 1219822
Adjusted points to 220
0
 

Accepted Solution

by:
heistad earned 220 total points
ID: 1219823
I have been asking for this very thing in the JDK. Currently there is no way to do it. They had promised us this for JDK1.1.1, but didn't get it done. Oh well, maybe next time. :(

-Chris
heistad@ncsa.uiuc.edu
National Center for Supercomputing Applications (NCSA)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question