0

I have a client server application. When multiple clients are connected to the server on a specified port say 6001, I am facing a problem. The problem is when the clients are connected to server and if I stop the server using a script with command kill -9 $pid (where pid is the server app pid), then while restarting the server I get an BIND Exception- Address already in use that means port 6001 is still not available for clients to connect again due to which clients fail to connect to server.

I was looking for a way to resolve this. I have one thought of using range of ports dynamically.

Here is what I am thinking :

In the Xml file instead of hard coding the port number as 6001. I will provide a range of ports say 6001-6005. Then when the server starts it will loop through the ports to choose the available port. For example - when the server tries to attempt to create a server socket bound to the specified port 6001, if there is an exception that if the port is already bound by another application, then it will choose 6002 port and so on. Whichever port will be available, the server will create the server Socket object for it and start listening for clients on that port.

Now similarly on client side it will first attempt to connect on 6001, if fails then 6002 and so on until it connects to right port on which server is listening to.

My question is if this is the right approach. Also if a client can connect in such a way mentioned above.

if No, then is there any there way to solve the issue which I mentioned above.

Vivek Rawat
  • 79
  • 1
  • 5
  • If you successfully kill the program the port should become available. Does your code launch a subprocess that handles incoming connections? If so, is that subprocess still running? – Jim Garrison Apr 23 '17 at 07:02
  • Well, I am not sure if there is any other process running. How can I find that ? My server is running on Solaris. The issue doesn't replicate on linux, neither on windows. Can you provide a command which can Kill all the process, including sub processes ? Below is the command which runs when I stop the server. # # Shutdown the server # kill_server() { pid=`server_pid` if [ "$pid" != "" ] ; then /bin/kill -9 $pid echo Server stopped. else echo Server is not running. fi } – Vivek Rawat Apr 23 '17 at 08:08

2 Answers2

1

You need to create your ServerSocket thus:

ServerSocket ss = new ServerSocket(); // NB no parameters
ss.setReuseaddress(true);
ss.bind(new InetSocketAddress(port));

This tells the operating system it's ok to bind the server socket even if there are left-over connections in TIME-WAIT state. If there is still a prior instance of the application running, of course you will need to stop it first.

Forget the port-range stuff.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • ss.bind(port); or ss.bind(new InetSocketAddress(port)); ? Also, What is the use of ss.bind ? what if I just use setReuseAddress ? by the way thanx for your response ! – Vivek Rawat Apr 24 '17 at 04:42
  • Yes, `bind(new InetSocketAddress(port))`. `setReuseAddress()` should *precede* `bind()`, and it is not a substitute for it. – user207421 Apr 24 '17 at 10:59
0

To find the pid of the process that uses a port use

lsof -wni tcp:portnumber

then kill the process using pid which will release the port for further use and it will not give the "already in" use error. This will solve your problem. Its better practice to have a single port for access from clients.

OTM
  • 656
  • 5
  • 8
  • I am using Solaris. I think this command doesn't work with solaris. Do you know the command for solaris ? – Vivek Rawat Apr 23 '17 at 08:18
  • Please see the link that talks about a script for lsof equivalent in solaris http://stackoverflow.com/questions/20670400/equivalent-lsof-i-in-solaris – OTM Apr 24 '17 at 04:20