简体   繁体   中英

socket_read() unable to read from socket 104 connection reset by peer php

I am getting data from a gps device in php socket. Device is connected to server but socket can't read data and given error like:

socket_read() unable to read from socket 104 connection reset by peer

My socket code:

$ip         = "107.191.105.101"; 
$port       = '5028'; 

if(!($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) 
{ 
    $errorcode = socket_last_error(); 
    $errormsg  = socket_strerror($errorcode); 

    die("Couldn't create socket: [$errorcode] $errormsg \n"); 
} 

echo "Socket created \n"; 

// Bind the source address 
/*if( !socket_bind($sock, $ip , $port) ) 
{ 
    $errorcode = socket_last_error(); 
    $errormsg  = socket_strerror($errorcode); 

    die("Could not bind socket : [$errorcode] $errormsg \n"); 
} */
socket_connect($sock, $ip , $port);
//  
echo "Socket bind OK \n"; 

//listen the socket 
if(!socket_listen ($sock , 10)) 
{ 
    $errorcode = socket_last_error(); 
    $errormsg  = socket_strerror($errorcode); 

    die("Could not listen on socket : [$errorcode] $errormsg \n"); 
} 

echo "Socket listen OK \n"; 


echo "Waiting for incoming connections... \n"; 

//array of client sockets 
$client_socks = array(); 
$max_clients  = 1000;
//array of sockets to read 
$read = array(); 

//start loop to listen for incoming connections and process existing connections 
while (true)  
{ 
    //prepare array of readable client sockets 
    $read = array(); 

    //first socket is the master socket 
    $read[0] = $sock; 

    //now add the existing client sockets 
    for ($i = 0; $i < $max_clients; $i++) 
    { 
        if($client_socks[$i] != null) 
        { 
            $read[$i+1] = $client_socks[$i]; 
        } 
    } 

    //now call select - blocking call 
    if(socket_select($read , $write , $except , null) === false) 
    { 
        $errorcode = socket_last_error(); 
        $errormsg  = socket_strerror($errorcode); 
        //
        die("Could not listen on socket : [$errorcode] $errormsg \n"); 
    } 

    //if ready contains the master socket, then a new connection has come in 
    if (in_array($sock, $read))  
    { 
        for ($i = 0; $i < $max_clients; $i++) 
        { 
            if ($client_socks[$i] == null)  
            { 
                $client_socks[$i] = socket_accept($sock); 

                //display information about the client who is connected 
                if(socket_getpeername($client_socks[$i], $address, $port)) 
                { 
                    echo "Client $address : $port is now connected to us. \n"; 
                } 
                break; 
            } 
        } 
    } 

    //check each client if they send any data 
    for ($i = 0; $i < $max_clients; $i++) 
    { 
        if (in_array($client_socks[$i] , $read)) 
        { 
             $input = socket_read($client_socks[$i] , 10240, PHP_BINARY_READ);  


            if ($input == null || $input === '')  
            { 
                //zero length string meaning disconnected, remove and close the socket 
                unset($client_socks[$i]); 
                socket_close($client_socks[$i]); 
            } 

            if(strlen($input) == 17)
            { 
                // Recieve data from tcp socket
                $payloadFromDevice = bin2hex($input);


                $input = socket_read($client_socks[$i] , 2048, PHP_BINARY_READ); 

                if ($input == null || $input === '')  
                { 
                    //zero length string meaning disconnected, remove and close the socket 
                    unset($client_socks[$i]); 
                    socket_close($client_socks[$i]); 
                } 


                $input = bin2hex($input); // from gps module rawdata bin to hex

                // Now we need to wait for next data from the device

                // Recieve next payload from the socket (now with data)
                $tcpPayloadFromDevice = $input;

                $res_write=socket_write($client_socks[$i], chr("00000002"));

                unset($client_socks[$i]);
            }

        } 
    } 
} 

My code is working fine it connects to the device but when I try to read data using socket_read() data is null or not read error. I have tried socket_recv() as well but still don't get data or I get the same error.

Safe to assume this is a client although your code contains both client and server instructions.

My understanding is:

  • the server would need to socket_create then socket_bind followed by socket_listen . Then for each connection it would need to socket_accept and finally socket_read (and socket_write ) from the connected client .
  • then client would also need to socket_create but it shouldn't bind or listen for anything, instead it will socket_connect to the server and socket_read and / or socket_write to that connection.

Don't forget to socket_close when each resource is no longer needed to avoid resource bleeding.

Feel free to check out a minimal project I wrote in PHP on this topic: https://github.com/mariuscucuruz/php-sockets-docker

Hope this helps.

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