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






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

 
<?
    $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);
?>
  
  








Related examples in the same category