简体   繁体   中英

How to identify client PHP Socket?

So I've got myself a regular PHP socket (More or less the same code from the example from the php manual). I have found a way to detect when a client disconnects (gracefully or not), but how can I identify who it was? IP address use is out because there might be more than one user with the same IP.

Thanks in advance.

If you think about what is delivered in a TCP or UDP packet header, there isn't much identity information included, just the IP address. If you want to know the identity of clients you need to have them send some sort of unique identifier (eg username & password as @madara commented). If they're from the same IP that means that they're using the same router in which case its purpose is to mask what devices are behind the router.

To detect who disconnected, you first need to determine who connected. Each connection gets its own socket, even if they're coming from the same IP address. In psuedo php:

// Store all active sockets in an array
$online_users = array();

// Open up a listening socket
$listener = socket_create(...);
socket_listen($listener);
$client_sock = socket_accept($listener);

// Have the client send authentication stuff after connecting and
// we'll receive it on the server side
$username = socket_read($client_sock, $len);
// Map the username to the client socket
$online_users[$username] = $client_sock;

// Continue to read or write data to/from the sockets. When a read or
// write fails, you just iterate through the array to find out who
// it was. If the socket $failed_sock failed, do as follows
foreach ($online_users as $name => $socket)
{
    if ($socket == $failed_sock)
    {
        // $name is the username of the client that disconnected
        echo $name . ' disconnected';
        // You can then broadcast to your other clients that $name
        // disconnected. You can also do your SQL query to update the
        // db here.
        // Finally remove the entry for the disconnected client
        unset($online_users[$name]);
    }
}

Logically in your case this is tough one! Here's just an idea:

If it's for a chat how about storing all online users in a database or flat file with the following columns:

NICKNAME
IP
TIME

And create a function that will check these and update accordingly the time let's say every 10 seconds. Based on that you'll be able to determine when and who's is online/offline.

------ UPDATE ------

Check your socket error? Use get_last_error() to check the error code.

$errorcode = socket_last_error(); 
$errormsg=socket_strerror($errorcode); 
die("Error: (".$errorcode.") ".$errormsg."\n");

Unset the user:

if($data === FALSE) {
    socket_close($clients[$i]['socket']);
    unset($clients[$i]);
    echo 'Client disconnected!',"\r\n";
    continue;
}

Unset the client from the database. You can also identify the exact nickname from the $clients array by their ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM