Home > Net >  How to reject duplicate PHP socket connection from the same client?
How to reject duplicate PHP socket connection from the same client?

Time:11-22

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);

CodePudding user response:

Keep a map of sockets to identities and close a (new) socket after identifying, when an identity is already in the map.

$map = [
  [socket1] => User(123),
  [socket1] => User(124),
];

CodePudding user response:

<?php

$address = '127.0.0.5';
$port = 8085;

$server = socket_create(AF_INET, SOCK_STREAM, 0) or die('Not Created');
socket_bind($server, $address, $port) or die("Not Binded");

socket_listen($server, 1) or die("Did not listen");

// clients before checking gamerId
$pending = [];
// accepted clients
$clients = [];
// gamerId list
$gamerIds = [];

// gamerId for socket
$clientsIds = new WeakMap();

echo "Listening...\n";

do {
    // wait for new client and new data on sockets
    $read = [$server, ...$pending, ...$clients];
    $write = null;
    $error = null;
    if(socket_select($read, $write, $error)){
        foreach($read as $socket){
            if($socket === $server){
                // new connection
                $pending[] = socket_accept($server);
                printf("New Socket connected\n");
            } else if(in_array($socket, $pending, true)) {
                // data for pending connection
                $readData = socket_read($accept, 1024);

                // remove key from pending
                if (($key = array_search($socket, $pending, true)) !== false) {
                    array_splice($pending, $key, 1);
                }

                // client disconnected already
                if($readData === false){
                    printf("Pending client disconnected #%d\n", (int)$socket);
                    socket_close($socket);
                    unset($socket);
                    continue;
                }

                // here should be something to extract gamerId (e.g. make sure it is X characters)
                $readData = trim($readData);

                if(in_array($readData, $gamerIds, true)){
                    printf("Pending client already connected #%d, blocked...\n", (int)$socket);
                    // close connection
                    socket_close($socket);
                    unset($socket);
                } else {
                    printf("Pending client accepted #%d\n", (int)$socket);
                    // accept client
                    $clients[] = $socket;
                    $gamerIds[] = $readData;
                    $clientsIds[$socket] = $readData;
                }
            } else {
                // client communication
                $readData = socket_read($accept, 1024);

                if($readData === false){
                    printf("Client disconnected #%d\n", (int)$socket);
                    // remove client
                    if (($key = array_search($socket, $clients, true)) !== false) {
                        array_splice($clients, $key, 1);
                    }

                    // remove gamerId from the list
                    if(isset($clientsIds[$socket])){
                        if (($key = array_search($clientsIds[$socket], $gamerIds, true)) !== false) {
                            array_splice($gamerIds, $key, 1);
                        }
                    }
                    socket_close($socket);
                    unset($socket);
                } else {
                    // handle data...
                    printf("Data received from socket #%d\n", (int)$socket);
                }

            }
        }
    }
} while (true);
  • Related