Sockets can be powerful

OK, so a ROT13 system is not very helpful, so I would not be surprised if you were wondering whether sockets could be made to do anything useful . Well, consider what socket applications you use - what relies on sockets to work?

One of the simplest systems is a web server - an application that listens for requests on a given port, and serves up the files that are asked for. Naturally web servers do a lot more than that - they have to receive POST data, handle file uploads, permissions, filters, and lots more, but we're only interested in the most basic task: serving files.

Your average HTTP request will look like this:

GET /somefile.txt HTTP/1.1
HOST: www.foo.com

Out of all that, we can ignore everything but "/somefile.txt" - that is the file we need to serve up. So, our server waits listening for connections and requests, sending out files as appropriate. Each HTTP response needs to take a very specific format because HTTP splits its response into "header" and "content". The header part can contain several lines of header information, such as the name of the file being served, its size, its type, the name of the server, etc, but it always contains the HTTP response type. The content part is the file itself, and is separated from the header by two sets of carriage return/line lines.

The HTTP response type is one line of text that contains the HTTP version being used (usually 1.0 or 1.1), followed by the status code of the response and a small amount of text explaining the status. The status code is meant for the web browser, whereas the text is meant for the person actually using it - browsers are under no obligation to display or indeed do anything with the text you send along with the status code.

The content type is particularly important because it tells browsers how to handle the file being sent - this uses a standard known as Multipurpose Internet Mail Extensions (MIME) to define the exact type of data. Some of the various MIME types will be discussed in more detail later - for now you just need to know that the MIME type for HTML is "text/html", and for PNG images it is "image/png".

Without further ado, here is a very basic web server written using PHP:

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

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

    while (true) {
        $client = socket_accept($socket);
        $input = trim(socket_read ($client, 4096));
        $input = explode(" ", $input);
        $input = $input[1];
        $fileinfo = pathinfo($input);

        switch ($fileinfo['extension']) {
            case "png";
                $mime = "image/png";
                break;
            default:
                $mime = "text/html";
        }

        if ($input == "/") {
            $input = "/index.html";
        }

        $input = ".$input";

        if (file_exists($input) && is_readable($input)) {
            print "Serving $input\n";
            $contents = file_get_contents($input);
            $output = "HTTP/1.0 200 OK\r\nServer: APatchyServer\r\nConnection: close\r\nContent-Type: $mime\r\n\r\n$contents";
        } else {
            $contents = "The file you requested does not exist. Sorry!";
            $output = "HTTP/1.0 404 OBJECT NOT FOUND\r\nServer: APatchyServer\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n$contents";
        }

        socket_write($client, $output);
        socket_close ($client);
    }

    socket_close ($socket);
?>

If you start that script up, you should be able to get to it through your web browser by visiting http://localhost:8000. You will need to put a little content in there to make it work at its best - if you create a small HTML file with PNG pictures in, the server should be able to serve it all up to a web browser.

 

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: Domain resolution functions >>

Previous chapter: Sockets can be servers

Jump to:

 

Home: Table of Contents

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