处理多个连接代码 [英] handling multiple connections code
问题描述
我需要您的帮助.
请理解这些代码中有关处理多个连接以及它们如何影响代码的几行.
I need your help.
Please I need to understand some lines in this code which about handling multiple connections and how they effect the code.
error_reporting(~E_NOTICE);
set_time_limit (0);
$address = "0.0.0.0";
$port = 5000;
$max_clients = 10;
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$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, $address , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
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();
//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";
}
//Send Welcome message to client
$message = "Welcome to php socket server version 1.0 \n";
$message .= "Enter a message and press enter, and i shall reply back \n";
socket_write($client_socks[$i] , $message);
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] , 1024);
if ($input == null)
{
//zero length string meaning disconnected, remove and close the socket
unset($client_socks[$i]);
socket_close($client_socks[$i]);
}
$n = trim($input);
$output = "OK ... $input";
echo "Sending output to client \n";
//send response to client
socket_write($client_socks[$i] , $output);
}
}
}
我不明白的话是:
第一行是$ read [0] = $ sock;在这里我不明白为什么编码器将主套接字存储在读取数组中.
第二行是if(in_array($ sock,$ read))在这里,我无法理解他/她如何使用此行检查新连接以及他/她如何检查主套接字(如果存在).读取数组,而他/她已经将其存储在读取数组中.
最后一行是if(in_array($ client_socks [$ i],$ read))在这里,我不明白为什么编码器在他/她想写的时候检查客户端套接字是否存在于读取数组中.它.
谢谢大家.
the lines which I can''t understand are:
the first line is $read[0] = $sock; here I can''t understand why the coder store the master socket in the read array.
the second line is if (in_array($sock, $read)) here I can''t understand how he/she check for a new connection using this line and how he/she check for the master socket if it is exist in the read array while he/she store it already in the read array.
the last line is if (in_array($client_socks[$i] , $read)) here I can''t understand why the coder check for the client socket if it is exist in the read array while he/she want to write for it.
thanks every body.
推荐答案
地址 = " ;
address = "0.0.0.0";
端口 = 5000 ;
port = 5000;
max_clients = 10 ; if(!(
max_clients = 10; if(!(
这篇关于处理多个连接代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!