Solved

so_reuseaddr for server  sockets

Posted on 1997-04-24
7
276 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 57
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
javap bin 2 35
Android development question 2 37
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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