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