I'm developing two applications in C++ that use C Linux socket calls, a server and a client. The server listens on a specific port A and retrieves a connection using the accept function. I'm using int result = ::listen(mySocketFileDescriptor, 1); limiting the maximum connections to 1. By the way, in the server I'm using the SO_REUSEADDR option to reuse the socket for other reasons.
If there are multiple disconnections/connections from the client, sometimes strange behaviours happen: for instance, the client successfuly connects to the server but then when it sends data, the server doesn't receive anything.
In the client application I connect to port A, using a Linux auto-assigned port, let's call it B. Using netstat I've discovered that the client is connected to the server to the port A both from a socket that uses the port B and from another one using another port C. I've debugged and I've seen that the server reads from the socket that uses B while the client writes on the socket that uses C.
Any idea about the cause of this behaviour?
Apart from any possible logical problem that my code may have, is it possible to make the server always discard an old connection when a new one is established? is there any option I can set on it?
Thank you in advance!