When connecting to the server, you will need a communication protocol so that you can check whether it is normal to start sending / receiving data, otherwise you can connect to one of the endpoints and immediately start sending data so that the other end cancels the connection.
To provide a single connection to the server, you just need something like this pseudocode:
remote_server = accept_connection()
lock mutex;
if(already_connected(remote_server)) {
drop_connection(remote_server)
}
unlock mutex;
If your server is not multithreaded, you do not need locks to protect you when you check whether you are already connected, because there will be no problems at the same time.
, , .
, , , (, ) . , , , - .