• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John McDowell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John McDowell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic