简体   繁体   中英

how to accept multiple connection to the device

I am creating a socket script that will listen to our 3 devices.and the device is setup in one server ip and one port only.

 $file = fopen('txt.log','a+');
    $server = stream_socket_server('tcp://'.$ipserver.':'.$port, $errno, $errorMessage);

    if(!$server) {
        echo "$errorMessage ($errno)<br />\n";
    }
    else{

        while($client = @stream_socket_accept($server,$timeout)) {
               stream_copy_to_stream($client, $file);
               fclose($file);
               fclose($client);


        }

    }

but the problem is that if one device is connected,the two devices cannot connect anymore.I appreciate some one can help me how to get this work.or give me some idea

Thank you in advance.

$file = fopen('txt.log', 'a+');
$server = stream_socket_server("tcp://$ipserver:$port", $errno, $errorMessage);
if (!$server)
    echo "$errorMessage ($errno)<br />\n";
else
{
    $s = array($server);
    $t = $timeout == -1 ? NULL : $timeout;
    while ($r = $s and stream_select($r, $n=NULL, $n=NULL, $t))
        foreach ($r as $stream)
            if ($stream == $server) // new client
                $s[] = stream_socket_accept($server, -1);
            else
                if (!fputs($file, fgets($stream)))
                {
                    fclose($stream);
                    array_splice($s, array_search($stream, $s), 1);
                }
}

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