Sockets can be servers

resource socket_create_listen ( int port [, int backlog])

resource socket_accept ( resource socket)

int socket_write ( resource socket, int length [, int length])

string socket_read ( resource socket, int length [, int type])

void socket_close ( resource socket)

We have just covered the basic use of sockets in PHP, and the next step is to move onto the advanced, more flexible sockets that allow you to create more advanced networking scripts with PHP.

There are several functions of interest here: socket_create_listen(), socket_accept(), socket_write(), socket_read(), and socket_close(). In order, those functions create and instruct a socket to listen on a given port, receive a client connecting to the socket, write text out to the client, read text sent by the client, and close the socket.

The sockets system starts with socket_create_listen(), which takes a port number to listen on as its only parameter. This function creates a socket, binds it to the port you specify, and returns a pointer to the socket it created or false if it failed. You will need the socket resource it returns for later functions, so you should always save it in a variable. If the function fails, it is likely because the port you specified is already being used, or perhaps because you have insufficient privileges to open the port.

The socket_accept() function takes the return value of socket_create_listen() as it is only parameters, and returns a client connection - someone who connected to our port number. Socket_accept() works by examining the queue of people waiting to be served, and taking the first client from there - if there are no clients waiting to be served, socket_accept() will wait ("block") until a client does become available, at which point it will return that.

Socket_write() takes two parameters, which are the client to write to and the value you want to write - this data is then sent through our socket to the client, as you would expect. Its partner, socket_read(), also takes two parameters, which are the connection to read from, and the number of bytes to read. By using socket_write() and socket_read() together, you can fully interact with clients connecting to your socket.

Here is an example script that creates a ROT13 server - when people connect to it and send text, it responds with the ROT13 equivalent of their text.

<?php
    $socket = @socket_create_listen("12345");

    if (!$socket) {
        print "Failed to create socket!\n";
        exit;
    }

    while (true) {
        $client = socket_accept($socket);
        $welcome = "\nWelcome to the Amazing ROT13 Machine.\nType '!close' to close this connection, or type '!halt' to halt the server.\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);
?>

Because this is going to serve data over a potentially infinite length of time, it is important you execute that script using the CLI SAPI, not your web browser. Once you have it running, bring up a new command-line window and enter the following:

telnet localhost 12345

That should launch your telnet program, which is useful for forming simple connections to servers. All being well, you should receive the welcome message from the ROT13 server - try it out with a few words, then type !shutdown to finish.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Sockets can be powerful >>

Previous chapter: Sockets aren't all about HTTP

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.