Several TCP servers listening on the same port
-
Does anybody know if it is allowed for more than one TCP server to listen at a specific port for in-coming connections?
I am using QTcpServer to listen on the same port for in-coming connections, but obviously only one of the servers is receiving the in-coming connection.
-
[quote author="Overflowz" date="1353673408"]Have you tried using threads?[/quote]
No. Basically different instances of the same application started. I came across the issue when using a server port to shut down gracefully.
If I remember correctly, I could shut down the first instance. However, even so, the port wasn't occupied anymore, the second instance did not receive later connects.[quote author="Luca" date="1353674676"]It's not possible.[/quote]
It is possible to close the server listening on a specific port, but leave the application running. Can the other application listen to port now? Any experience already? -
[quote author="koahnig" date="1353683205"]It is possible to close the server listening on a specific port, but leave the application running. Can the other application listen to port now? Any experience already?[/quote]
The other application needs to reopen the port explicitly after the port is freed. You can't listen to a port that is already open by another application, you'll receive an error if you try (QTcpServer::listen would returns false, and QTcpServer::errorString would say why it returned false). -
You can technically re-open a port that is already opened (using the SO_REUSEADDR socket option), but it may lead to undefined behaviour if both applications use the socket simultaneously.
It is amongst others used to "pick up" the port if the application has died and the port hasn't been released yet by the system, and the other one should step in - which might be the source of error in your case.
UDP multicast sockets allow for multiple applications subscribing to the same port simultaneously if this is an option for you.