I want to reject duplicate socket connection when the same connected client try to connect again.
The below code I tried to store gamerId into an array then later check the array if new gamerId already exist or not. But seems the duplicate connection already made but I don’t want to make any duplicate connection.
$address = '127.0.0.5';
$port = 8085;
$sock = socket_create(AF_INET, SOCK_STREAM, 0) or die('Not Created');
$bind = socket_bind($sock, $address, $port) or die("Not Binded");
$listen = socket_listen($sock, 1) or die("Didnot listen");
$accept = socket_accept($sock) or die("Not Accepted");
$readData = trim(socket_read($accept, 1024));
$gamerId = array();
$errHandler = array();
$gamerIdlen = count($gamerId);
function checkDuplicate($gamerId, $gamerIdLen, $readData, $errHandler)
{
for ($i = 0; $i < $gamerIdLen; $i++) {
if ($gamerId[$i] === $readData) {
return 1;
}
}
}
if (checkDuplicate($gamerId, $gamerIdlen, $readData, $errHandler) == 1) {
array_push($errHandler, "exist");
} else if (checkDuplicate($gamerId, $gamerIdlen, $readData, $errHandler) != 1) {
array_push($gamerId, $readData);
}
do {
global $accept;
$accept = socket_accept($sock) or die("Not Accepted");
print_r($errHandler);
print_r($gamerId);
} while (true);
2
Answers
Keep a map of sockets to identities and close a (new) socket after identifying, when an identity is already in the map.