Solved

so_reuseaddr for server  sockets

Posted on 1997-04-24
7
271 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now