Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 在php中使用多个客户端读取多个套接字_Sockets - Fatal编程技术网

Sockets 在php中使用多个客户端读取多个套接字

Sockets 在php中使用多个客户端读取多个套接字,sockets,Sockets,我的插座连接有问题 我需要多个客户端读取多个套接字,但我可以连接多个客户端,但只能为客户端读取一个套接字,如果我添加第二个套接字读取,程序将无法工作 $sock = socket_create(AF_INET, SOCK_STREAM, 0); $socket_bind($sock, $address , $port); $socket_listen ($sock , 10) while (true){ $read = array(); //first socket is the ma

我的插座连接有问题

我需要多个客户端读取多个套接字,但我可以连接多个客户端,但只能为客户端读取一个套接字,如果我添加第二个套接字读取,程序将无法工作

$sock = socket_create(AF_INET, SOCK_STREAM, 0);
$socket_bind($sock, $address , $port);
$socket_listen ($sock , 10)

while (true){ 


$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_getsockname($client_socks[$i], $address, $port))
            {

                echo "Client $i : $port . \n";
            }

            //Send Welcome message to client
           $msg = "Bienvenido cliente {$key[0]} \n\r";
     $msg .= "1.-Crear Paleta\n\r";
     $msg .= "2.-Ubicar Paleta\n\r";
     $msg .= "exz.-Salir\n\r";
            socket_write($client_socks[$i] , $msg);

            break;
        }
    }
}

//check each client if they send any data
for ($i = 0; $i < $max_clients; $i++)
{

    if (in_array($client_socks[$i] , $read))
    {


        if (false === ($input = socket_read($client_socks[$i],PHP_NORMA))) {
        echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
        break 2;
    }


        echo "client $i : ";
        echo "$input";

        //send response to client
        socket_write ($client_socks[$i], "client $i : ");
        socket_write ($client_socks[$i], $input);

        socket_write ($client_socks[$i], "\r");
        $y++;
    }
}
$sock=socket\u创建(AF\u INET,sock\u STREAM,0);
$socket\u bind($sock,$address,$port);
$socket\u监听($sock,10)
虽然(正确){
$read=array();
//第一个插座是主插座
$read[0]=$sock;
//现在添加现有的客户端套接字
对于($i=0;$i<$max\u客户端;$i++)
{
如果($client_socks[$i]!=null)
{
$read[$i+1]=$client_socks[$i];
}
}
//现在调用select-阻塞调用
if(socket_select($read,$write,$except,null)==false)
{
$errorcode=socket\u last\u error();
$errormsg=socket\u strerror($errorcode);
die(“无法侦听套接字:[$errorcode]$errormsg\n”);
}
//如果ready包含主套接字,则新的连接已进入
if(在数组中($sock,$read))
{
对于($i=0;$i<$max\u客户端;$i++)
{
如果($client_socks[$i]==null)
{
$client_socks[$i]=socket_accept($sock);
//显示有关已连接的客户端的信息
if(socket_getsockname($client_socks[$i],$address,$port))
{
回显“客户端$i:$port.\n”;
}
//向客户发送欢迎信息
$msg=“Bienvenido客户{$key[0]}\n\r”;
$msg.=“1.-Crear Paleta\n\r”;
$msg.=“2.-Ubicar Paleta\n\r”;
$msg.=“exz.-Salir\n\r”;
socket_write($client_socks[$i],$msg);
打破
}
}
}
//检查每个客户端是否发送任何数据
对于($i=0;$i<$max\u客户端;$i++)
{
if(在数组中($client\u socks[$i],$read))
{
if(false==($input=socket\u read($client\u socks[$i],PHP\u NORMA))){
echo“socket\u read()失败:原因:”.socket\u strerror(socket\u last\u error($msgsock))。“\n”;
破口2;
}
echo“客户$i:”;
回显“$input”;
//向客户端发送响应
socket_write($client_socks[$i],“client$i:”);
socket_write($client_socks[$i],$input);
套接字写入($client\u socks[$i],“\r”);
$y++;
}
}
}