1
votes

I know there are already several similar questions, but I didn't find a reply that fits my case. Please consider a simple client-server app where I have to set up a TCP connection.

On Server side I wait for TCP connection by the code:

ServerSocket mySS= new ServerSocket(10000);
Socket mySocket_ServerSide= mySS.accept();                

On Client side I request for TCP connection by the code:

Socket mySocket_ClientSide= new Socket(); 
mySocket_ClientSide.setReuseAddress(true);
mySocket_ClientSide.bind(new InetSocketAddress(5555)); //always using the same port
mySocket_ClientSide.connect(new InetSocketAddress(serverIP,10000), myTimeout);

The first time everythink works, I do the job on TCP connection (for example a file transfer) and then I close (on client side) the connection to the server using:

mySocket_ClientSide.close(); 

After some seconds I have to connect again to the server, but I get an "address already in use: connect" exception.

I suppose that the state of the client process is in TIME_WAIT, as I read in several questions of this site. But shouldn't I be able to use this port again because of setReuseAddress(true)?

For some reasons I have to bind always to the same port (in the example 5555). Is there any solution that allows me to bind to the same port? Which mistake am I doing?

Thank you in Advance Fausto

2
What are the "some reasons"?David Schwartz
For what reasons? They will prevent you from making multiple connections, or even sequential connections less than two minutes apart. If this is a netadmin-imposed firewall constraint, have it lifted. It adds nothing to security, and it just causes immense programming problems. NB Surely the error is 'address already in use: bind'?user207421
Hi, thank you for your reply. OS is WIndows. My App is designed at the moment with this requirement of fixed port. I have to re-design it and do some work to change it... But I understood trom answer that I have to do it...Fausto70

2 Answers

1
votes

Your question embodies mutually inconsistent requirements:

  • You have to use the same source port
  • You have to reconnect within seconds.

As you have guessed, you have run into the TIME_WAIT state. This is a compulsory two-minute gap between successive connections from the same source IP: port to the same target IP:port. The rules of TCP require this gap so as to assure connection integrity.

You can reduce the TIME_WAIT period on some operating systems, but this is not recommended. Instead you should get rid of the requirement to always use the same port. There is no point in this, and it also prevents you from forming multiple connections in parallel.

0
votes

Your server can only ever accept a single connection. You need to call (Server)Socket.accept() in a loop. The accept() method blocks until a connection is made.

On your client, you should never bind the client-side endpoint to a specific port, unless you have an absolute good reason to do so. Remove these lines:

mySocket_ClientSide.setReuseAddress(true);
mySocket_ClientSide.bind(new InetSocketAddress(5555)); //always using the same port