Solved

so_reuseaddr for server  sockets

Posted on 1997-04-24
7
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …
Suggested Courses

632 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