PHP/Network/socket create listen

Материал из Web эксперт
Перейти к: навигация, поиск

Creates a ROT13 server when people connect to it and send text, it responds with the ROT13 equivalent of their text:

   <source lang="html4strict">

<?

   $socket = socket_create_listen("12345");
   if (!$socket) {
           print "Failed to create socket!\n";
           exit;
   }
   while (true) {
           $client = socket_accept($socket);
           $welcome = "\nWelcome\n";
           socket_write($client, $welcome);
           while (true) {
                   $input = trim(socket_read ($client, 256));
                   if ($input =  = "!close") {
                           break;
                   }
                   if ($input =  = "!halt") {
                           socket_close ($client);
                           break 2;
                   }
                   $output = str_rot13($input) . "\n";
                   socket_write($client, $output);
                   print "Them: $input, Us: $output\n";
           }
           socket_close ($client);
   }
   socket_close ($socket);

?>

 </source>