Question

I have a TCP server/client set up and they communicate the way I generally want.

What I want to do now is add functionality that would allow the client to automatically reconnect to the server once a connection is lost. I'm having trouble finding complete information online on how to do this.

Details: -I only have 1 server and so far it only hosts at most 1 connection. -When either the client OR the server disconnects - both close (is it sensible to close both the server's and client's sockets like this?)

Ideally, I would like for the client to be able to reconnect to the same port on the server each time. I understand TIME_WAIT and why it is necessary. I don't really want to use SO_REUSEADDR unless I don't have another option. How is this generally handled? Should I open a new port on the Server side while it waits for the old socket to finish TIME_WAIT and basically alternate between the two ports (which means that the client would have to keep track of 2 possible ports)?

Thanks for your suggestions !

Was it helpful?

Solution

TIME_WAIT is state of TCP connection, not the port. Every TCP connection identifies by tuple (local-address, local-port, remote-address, remote-port). So if the client connect to server using new (dynamic) local port then new TCP connection is created and TIME_WAIT isn't issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top