Why do I close the socket, of one page but the socket of other pages is also closed?

I use php"s socket to do a websocket service. There are two pages open and there are two socket resources. I get the input through socket_recv, and then judge whether the socket has been closed by judging whether the content returned by socket_recv is 0. But I only closed the socke of one of the pages, but both closed

.
Resource id -sharp6--------SEND------
Resource id -sharp5--------SEND------
Resource id -sharp6========CLOSE=======0
Resource id -sharp5========CLOSE=======0

using php to do websocket in mac

related codes

error_reporting(E_ALL | E_STRICT);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, "0.0.0.0", 9999);
socket_listen($socket);
$clients[] = $socket;
while (true) {

    $reads = $clients;//
    $write = null;
    $except = null;

    if (socket_select($reads, $write, $except, null) < 1) {
        continue;
    }
    if (in_array($socket, $reads)) {
        $new_connection = socket_accept($socket);
        $clients[] = $new_connection;
        hands(socket_read($new_connection, 2048), $new_connection);
        $key = array_search($socket, $reads);
        unset($reads[$key]);
    } else {

        foreach ($reads as $k => $socket_item) {
            //socket---socket
            $status = socket_recv($socket_item, $buffer, 2048, 0);
            if ($status < 1) {
                unset($reads[$k]);
                $key = array_search($socket_item, $clients);
                unset($clients[$key]);
                socket_close($socket_item);
                echo "CLOSE======>{$socket_item}----end---" . PHP_EOL;
            } else {
                $msg = decode($buffer);
                $content = frame($msg);
                foreach ($clients as $client_socket) {
                    if ($client_socket != $socket && $client_socket != $socket_item) {
                        @socket_write($client_socket, $content, strlen($content));
                    }
                }
            }

        }
    }
}

in my opinion, when I close one of the pages, it will only close one of the socket, but I do see that both of them are closed

.
0====>Resource id -sharp5
35====>Resource id -sharp6
0====>Resource id -sharp6

I tested socket_select. When I closed a browser called tab, socket_select executed it twice, so I turned off 6.

Php
Feb.16,2022

I wrote a script test, normal use will not occur such a disconnect, all disconnect

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(socket_connect($socket,'127.0.0.1',9999) == false){
    echo 'connect fail:'.socket_strerror(socket_last_error());
}else{
    $message = 'i love chengqm';
    socket_write($socket,$message,strlen($message));
    while(true){
        while($msg = socket_read($socket,1024)){
            echo 'server message is:'.PHP_EOL.$msg;
        }
        sleep(0.5);
    }
}
socket_close($socket);

is this a single-process, single-threaded program?


have you solved the problem? you have also encountered this problem.

Menu