0

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!

user3770392
  • 453
  • 5
  • 12

2 Answers2

2

You need to read the listen manual page more thoroughly, because the "limit" is not the max number of connections that can be made to that socket, it's the limit of the number of connections that can attempt to connect simultaneously before you call accept. Once you call accept another connection can be made.

The "standard" value in many examples is 5, and yet those servers can handle hundreds of connections.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • You're right, my fault. I'd like to read some other TCP/IP book, please feel free to suggest me one. In any case, I think this is not the problemas in the server, before doing the accept I close and delete the old socket – user3770392 Sep 14 '15 at 16:48
  • about books, I was thinking to read: W. Richard Stevens, Unix Network Programming, Volume 1: The Sockets Networking API but it seems quite old – user3770392 Sep 14 '15 at 16:49
  • 2
    @user3770392 it's still one of the definitive text books in this field. – Alnitak Sep 14 '15 at 16:50
1

It is not straightforward for the machine at one end of a network connection to recognize that the other end has disconnected. In particular, it is very difficult to do so if the remote end severs the connection abruptly, rather than properly closing it.

Furthermore, nothing inherently prevents the same two machines from establishing multiple simultaneous connections. If you want to restrict that to just one, then your server needs to track which client(s) it is currently connected to, and it must be prepared to service multiple connections at the same time, so as to recognize when a new connection is established by an existing client. That requires either multiplexing (e.g. with use of select()) or multiprocessing. Then, if it receives a new connection from a client that already has one, the server halts handling of the old connection and closes it.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • In my tests there's always the same client, so same ip address. When you're speaking about "handling of the old connection" what do you mean? I'm already closing the old socket every time but it seems not fixing the problem – user3770392 Sep 14 '15 at 17:03
  • @user3770392, the netstat test you describe shows that the server machine has two connections with the client established at the same time. At least on its end it does -- it may be that one of those is closed on the client side, but evidently the server does not recognize that. The one established first is the "old" one. The server *program* may not yet have `accept()`ed the new connection, but that's an altogether different issue. The server must `accept()` the new connection before it can recognize that it should drop the old one. That's why you need to handle multiple connections. – John Bollinger Sep 14 '15 at 17:14
  • @user3770392, also, be sure that you handle error results from all your function calls. It may be that the system already told the server program about the disconnection, but the server ignored it. – John Bollinger Sep 14 '15 at 17:16
  • Bollinger: thank you for your suggestions. Further information: the PID of the client has 2 established connections while the server just one (with the old port). I have just tried to do the accept and then the destruction of the old socket (saving it with a pointer) but still same problem. – user3770392 Sep 15 '15 at 08:09