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

    z99,

    Generally PHP isn't intended for server/daemon development. There are MANY languages that are better suited for this type of application.

    C, for instance, is generally what one would use under Linux, Windows or Mac OS.

    However, if you do want to do this type of development in PHP, I can suggest a book:

    You want to do WHAT with PHP?

    Note: Amazon URL provided for point of reference, not that I am saying you must get it from there 🙂

    I can't stress enough that Daemon development is intended for advanced developers, and this is even more so important when attempting to use PHP for htis.

    Also - that book is intended for very advanced developers, and you may need to dedicate quite a bit of time per-chapter.

    Best of luck.

      as i'm new to socket programming and start with php,i just want to finish a real server with php to understand things better in socket programming' world,and then move to C,Perl and etc.,anyway thanks for helping i think i have to start with something new

        Write a Reply...