Creating a Server

Now go back to the earlier example and complete it. To do this you simply need to listen to a particular socket and then process the user(s) connecting to it.

<?php
$commonProtocol = getprotobyname(“tcp”);

$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);

socket_bind($socket, ‘localhost’, 1337);

socket_listen($socket);

// Accept any incoming connections to the server
$connection = socket_accept($socket);
if($connection)
{
    socket_write($connection, “You have connected to the socket...

”);
}
?>

You will want to run this example from a command prompt. The reason for this is that you are creating a server, not a Web page. If you try to run this script from a Web browser, the script will most likely time out after 30 seconds. You could set an infinite time out by using the following line of code, but it is better to run the server from a command prompt:

set_time_limit(0);

To run your scripts from a command line you simply type:

php.exe example01_server.php

If you have not mapped the path to the PHP interpreter you will need to specify the path before php.exe. Once you have started the server you can test the connection by telneting to port 1337. You should see results that resemble Figure 10.2

Figure 10.2. Your first server.


The problem with this server is threefold: One, it doesn’t accept multiple connections. Two, it performs only one command that is very useful. Finally, you cannot yet connect to this server through your Web browser.

The first problem is easy to solve if you were using an application that didn’t have to reconnect to the server every time you clicked something. But since you are using a Web page to connect to the server, this poses a very large problem. You have to make your server accept a connection, write data to the client (if there is any to write), close the connection, and wait for another connection.

Let’s do exactly that. Take a look at the following code example to see the new server:

<?php
// Set up our socket
$commonProtocol = getprotobyname(“tcp”);

$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);

socket_bind($socket, ‘localhost’, 1337);

socket_listen($socket);

// Initialize the buffer
$buffer = “NO DATA”;

while(true)
{
    // Accept any connections coming in on this socket
    $connection = socket_accept($socket);
    printf(“Socket connected
“);

    // Check to see if there is anything in the buffer
    if($buffer != “”)
    {
        printf(“Something is in the buffer...sending data...
”);
        socket_write($connection, $buffer . “
”);
        printf(“Wrote to socket
”);
    }
    else
    {
        printf(“No Data in the buffer
”);
    }

    // Get the input
    while($data = socket_read($connection, 1024, PHP_NORMAL_READ))
    {
        $buffer = $data;
        socket_write($connection, “Information Received
”);
        printf(“Buffer: “ . $buffer . ”
”);
    }

    socket_close($connection);
    printf(“Closed the socket

”);
}
?>

This is what the server does. It initializes the socket and the buffer that you use to receive and send data. Then it waits for a connection. Once a connection is created it prints “Socket connected” to the screen the server is running on. The server then checks to see if there is anything in the buffer; if there is, it sends the data to the connected computer. After it sends the data it waits to receive information. Once it receives information it stores it in the data, lets the connected computer know that it has received the information, and then closes the connection. After the connection is closed, the server starts the whole process again.

Take a look at Figure 10.3 to see the results of the server.

Figure 10.3. Results of the new server.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset