aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes How do I unbind a server port once it has been used by my program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How do I unbind a server port once it has been used by my program?" Watch "How do I unbind a server port once it has been used by my program?" New topic
Author

How do I unbind a server port once it has been used by my program?

John McDowell
Greenhorn

Joined: Jun 14, 2014
Posts: 26
I coded the chat application in headfirst java and the program ran correctly the first time but when i ran it the second time,it gave bind exception. Could somebody please tell me how to unbind the port. Please don't direct me to some site with tons of documentation.The code I wrote is as follows:




Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

John McDowell wrote:I coded the chat application in headfirst java and the program ran correctly the first time but when i ran it the second time,it gave bind exception. Could somebody please tell me how to unbind the port. Please don't direct me to some site with tons of documentation.The code I wrote is as follows:


I am assuming that you mean the accept port -- the bind port will automatically be released when the application closes the socket. However, many OSes will place a timeout on the port to prevent it from being reused too quickly. To stop this, use the TCP reuseaddr option when creating the socket.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John McDowell
Greenhorn

Joined: Jun 14, 2014
Posts: 26
Henry Wong wrote:
John McDowell wrote:I coded the chat application in headfirst java and the program ran correctly the first time but when i ran it the second time,it gave bind exception. Could somebody please tell me how to unbind the port. Please don't direct me to some site with tons of documentation.The code I wrote is as follows:


I am assuming that you mean the accept port -- the bind port will automatically be released when the application closes the socket. However, many OSes will place a timeout on the port to prevent it from being reused too quickly. To stop this, use the TCP reuseaddr option when creating the socket.

Henry


I used the resuseaddr and set it to true, it still doesnt work
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

John McDowell wrote:
I used the resuseaddr and set it to true, it still doesnt work


You need to set the reuseaddr option before you bind the port -- which means you can't specify the rendezvous port in the constructor. Use the constructor that doesn't specify the port, set the reuse option, and then bind the port.

Henry
Domi Dori
Greenhorn

Joined: Mar 23, 2014
Posts: 6
    
    2
In java 7_25 oracle made some changes, you have to set
System.setProperty("sun.net.useExclusiveBind", "false");
additionaly.

See: http://www.oracle.com/technetwork/java/javase/7u25-relnotes-1955741.html
John McDowell
Greenhorn

Joined: Jun 14, 2014
Posts: 26
Henry Wong wrote:
John McDowell wrote:
I used the resuseaddr and set it to true, it still doesnt work


You need to set the reuseaddr option before you bind the port -- which means you can't specify the rendezvous port in the constructor. Use the constructor that doesn't specify the port, set the reuse option, and then bind the port.

Henry

What I gather from your comment is that I should first write the setReuseAddress(true); statement and then write the ServerSocket socket = new ServerSocket("127.0.0.1",5000); statement.
Am I right?

Also what if I have previously run a program(in which I forgot to add the setReuseAddress(true) function ) which has already bound the port 5000 to server socket.Is there a statement out there that unbinds a previously bound port ? Or is there no other option but to wait for the time out period?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

John McDowell wrote:
What I gather from your comment is that I should first write the setReuseAddress(true); statement and then write the ServerSocket socket = new ServerSocket("127.0.0.1",5000); statement.
Am I right?


No. Of course, that's not right. Think about it. How can you call a method on an instance before you instantiate that instance?

Take a look at the the ServerSocket class. You will see that there is a constructor that lets you create an unbounded server socket. There is also a method that lets you bind that unbound server socket to an address and port. Create an unbound server socket. Set the reuse to true. And then bind the address and port.

John McDowell wrote:
Also what if I have previously run a program(in which I forgot to add the setReuseAddress(true) function ) which has already bound the port 5000 to server socket.Is there a statement out there that unbinds a previously bound port ? Or is there no other option but to wait for the time out period?


I don't think that this would be an issue -- how the previously run application is executed shouldn't have an effect on how the new application reuses the port.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I unbind a server port once it has been used by my program?