All pastes #394590 Raw Edit

Stuff

public php v1 · immutable
·published 2007-03-14 13:16 UTC
rendered paste body
<?phphandle_client($clients,$read_sock,$data) {$buffer_in = explode(",", $data);switch($buffer_in[0]) { case "ping":  socket_write($read_sock,"Pong\n"); break;  case "hello":  socket_write("Hello!\n"); break;}   $port = 1234;   $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);   socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);   socket_bind($sock, 0, $port);   socket_listen($sock, 5);   $clients = array($sock);   while (true) {       $read = $clients;     if (socket_select($read, $write = NULL, $except = NULL, 1) < 1)           continue;       if (in_array($sock, $read)) {           $clients[] = $newsock = socket_accept($sock);           socket_write($newsock, "Hello.. welcome to this server\n\r");           $key = array_search($sock, $read);           unset($read[$key]);       }       foreach ($read as $read_sock) {          echo "Now reading..\n";           $data = socket_read($read_sock, 1024, PHP_BINARY_READ);          echo "Done reading...\nGot: $data \n";           if ($data == "") {               echo "\nOne client has left the building..";               $key = array_search($read_sock, $clients);               unset($clients[$key]);               continue;           }           $data = trim($data);           if (!empty($data)) {//             array_shift($clients);               handle_client($clients,$read_sock,$data);           }       } // end of reading foreach   }   // close the listening socket   socket_close($sock);?>