Connection overriding when running php socket script

0

I'm building a socket server in php, a simple chat. When I send a message or close a page, the following error appears: " Warning: socket_recv () Unable to read from socket [10053]: An established connection has been aborted by the software on the host computer " and in the other line with the same message, only with socket_write (), this only happens when I put socket_write to send the messages to all clients, see a snippet of code:

foreach($read as $read_socket){
$bytes = socket_recv($read_socket, $data, 1024, null);
if($bytes === 0){
    $key = array_search($read_socket, $array_clients);
    unset($array_clients[$key]);
    echo PHP_EOL . "Cliente desconectado...";
}else{
    foreach($array_clients as $clients)
        socket_write($clients, $data, strlen($data));
}

}

ThisisverystrangesinceIhavealreadydisabledthefirewallandavastandtheerrorcontinues.Wheneveraclientdisconnectsandsocket_write()isnotpresent,themessage"Client disconnected ..." appears normally, but in case of an echo $ data inside the else to show messages from other clients, "♥ Ú" and then the message saying that the client was disconnected. Could you help me?

    
asked by anonymous 12.07.2018 / 21:26

1 answer

0

I made a change in the code and ran, logically that was a gambiarra and that can give problem at any time because I realized that the strange symbol "♥ Ú" returns 8 bytes for the function socket_recv, so I had to put an if to avoid the sending it, but like I said, this was a bummer because short messages with less than 3 characters are not sent.

foreach($read as $changed_socket){
            $bytes = socket_recv($changed_socket, $data, 2048, null);

            if($bytes === 0){
                unset($array_clients[array_search($changed_socket, $array_clients)]);
                echo "Cliente $changed_socket desconectado.";
            }elseif($bytes === false){
                $this->sock_error();
            }else{
                if($bytes>8){
                    $msg = "MSG($changed_socket):" . $this->unmask($data) . "<br />";
                    $msg = $this->mask($msg);
                    foreach($array_clients as $conectados)
                        socket_write($conectados, $msg, strlen($msg));
                }
            }

        }
It is as follows, when the client disconnects, first socket_recv receives a message (this should not happen) which in the case is these strange symbols "♥ Ú", in the next loop it really disconnects the client and displays the message "Client disconnected ...". This is very strange, as it would be, as soon as socket_select realizes a change in $ read, it should immediately pass the socket that wants to close the connection, however it only does this in the next loop, how can I solve this problem? Can it be memory junk?

    
13.07.2018 / 03:58