This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:




 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It's a tiny ad. At least, that's what she said.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic