简体   繁体   中英

asynchronous socket connection in php

i'm trying write asynchronous socket listener code in php. but listener response answer only for first request and for other requests it only can receive packets without response (i'm checking with sniffer) also i'm counting how many time loop was active and loop is active only for first request ...... i'll show my Code :

addr = '192.168.0.117';
$port = 7878;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $addr, $port) or die('Could not bind to address');
socket_listen($sock);

$null = NULL;
$clients = Array();
$cc = 0; // loop counter

while(true){

    echo $cc."<br>";
    $cc = $cc +1;

    $read[0] = $sock;

    $ready = socket_select($read,$null,$null,$null);    
    $client = socket_accept($sock);
    $input = socket_read($client, 312); 


    echo $input;

    if($input == "exit"){
        socket_close($client);
        socket_close($sock);
        return false;
    }

    $output = 0x11;
    socket_write($client,$output);
    $input = "";
}

May be you are closing the listening socket right after the first client sends exit command. You should close only client socket when the client sends exit command. And you return false from the while loop. This means obviously it will process only one client.

if($input == "exit"){
   socket_close($client);
   socket_close($sock);  // Think about this Line.
   return false; // Think about this Line.
}

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