Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

so_reuseaddr for server  sockets

Posted on 1997-04-24
7
Medium Priority
?
286 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 440 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.
Suggested Courses

715 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