1

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

3
  • What are the "some reasons"? Commented Feb 28, 2017 at 19:29
  • 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'? Commented Feb 28, 2017 at 19:31
  • 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... Commented Mar 1, 2017 at 4:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

. first of all thank you. As written in above other comment, I have to re-design it in order to not use the same port. At least it could be usefull to limit to a range of port (so that the firewall can open a limited number of ports instead to open all the ports), but I just saw that doesn't exist a such command. Therefore my idea now is, on client side, to pass to bind a random number in a range, and try again if the port is not available. I'd apprieciate a your opinion about it... I'm thinking that the problem to not open all the ports in a firewall should be common, or not?
You can't limit it to a range of ports. There is no API for that. There is no reason not to allow all source ports in a firewall. All that such rules accomplish is impossibility or inconvenience for the applications, as you are discovering. There is no benefit whatsoever of any kind, whether security or otherwise.
Hi @EJP. There is one use case where it's the main point: STUN. I'm reviewing an implementation made under Java 1.6 specs. So far, I'm running into several errors, mostly either with binding or connecting through the same address and port, necessitating amendments to the code e.g. soLinger(true, 0) Has anything changed since then (Java 6 to 8)? This code was signed off as user-ready, coming from a reputable source, but it broke in so many places when I tested. I'm resisting the idea of downgrading the server environment to 1.6, since other apps in it are on 1.8 specs.
Also, I was wondering how other implementations out there got around this issue? Google has several STUN servers up, and I think Chrome is a client.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.