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