hi everyone,i write a simple server that can handle more than one client and also will get each client's data and will show them,but i can't get the data from client,i'm new to socket programming,that's my code:
<?php
//Server
set_time_limit(0);
$host='127.0.0.1';
$port=9997;
$socket=socket_create(AF_INET,SOCK_STREAM,0);
socket_bind($socket,$host,$port);
socket_listen($socket);
$client=array($socket);
while (true) {
$read=$client;
$cliid=0;
socket_select($read,$write=NULL,$except=NULL,$tv_sec=NULL);
if(in_array($socket,$read)) {
for($i=1;$i<5;$i++)
{
if(!isset($client[$i]))
{
$client[$i]['socket']=socket_accept($socket);
$cliid+=1;
socket_getpeername($client[$i]['socket'], $ip);
echo "Client #".$cliid." With IP {$ip} Connected\n";
$msg="\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'.\n";
socket_write($client[$i]['socket'],$msg);
} //inside if
} //for
break;
} //first if
if(in_array($socket,$client)){
for($i=1;$i<5;$i++){
$data=socket_read($client[$i]['socket'],1024);
if(trim($data)!=''){
echo 'getting data';
echo $data;}
}}
} //while
socket_close($socket); //for accepting
?>
my first question is:
should i really use $read=$client,cause i saw on a web-page that the socket_select may change the $client,so i don't use the $read to see if there will be a problem [only use the $client] and there was no problem with accepting the client(s)
thanks in advance