In order to introduce the core concepts of socket programming in PHP in a friendly way, I'm going to set up a basic example, which hopefully will help to illustrate the usage of the most important socket-related PHP functions. First I'll show you how to develop a simple TCP server with sockets, and then I'll explain the meaning of each function used in the sample script.
Having said that, here's a procedural script that builds a basic TCP server. Take a look at the code listed below:
// define TCP port & local host
As you can see, the procedural script listed above demonstrates how to create a simple TCP server, where its only functionality rests in taking up a string sent by a client, then converting it to its uppercase version and finally returning it to the mentioned client. Does that sound a little bit confusing? Don't worry, it really isn't.
First, the script defines the host and port number where the TCP server will be running. In this specific case the server will be created to work as "localhost," that is its IP address will be 127.0.0.1 and it will run on the TCP port "1234."
After defining the corresponding connection parameters, the first connection socket is created by the "create_socket()" PHP built-in function, which obviously creates and returns a socket reference. Also, the first argument of this function--that is the domain parameter--specifies the protocol family to be used by the socket, in this case AF_INET, which means that it will use the IPv4 Internet-based protocols.
Additionally, the second argument indicates the type of socket to be used. In this example, I specified a value of "SOCK_STREAM," meaning that the socket will support the use of the TCP protocol. Finally, the last function parameter determines the protocol used by the socket. In this case I decided to use the reliable TCP protocol.
Of course, all the above description can be assumed in the following statement:
// create low level socket
Right, after creating the general connection socket, the script needs to bind it to the host and port defined before. To perform this operation, the "socket_bind()" function is conveniently used, as listed below:
// tie up socket to TCP port
At this point, I created a general connection socket, which is tied to the 127.0.0.1 IP address, and will be expecting incoming connections on the TCP port 1234. Now, how do I make the socket listen to client connections? Right, you guessed it, the "socket_listen()" function does precisely that:
// begin listening connections
Now that the general connection socket has been created and it's expecting client connections, there are a few additional things to be done yet. The first one consists of creating another communication socket, which will handle all the operations for reading and writing data. This is achieved by the "socket_read()" and "socket_write()" PHP functions respectively, and the entire reading-writing sequence is illustrated below:
// read socket input
As you can see, the above code block first reads the data sent by the client, then converts the data to uppercase and writes the string back to the client. At the end of this process, both sockets, the general and the communication one, are closed by the "socket_close()" PHP function, as shown below:
// close sockets
Wasn't that simple and fun? With a few PHP functions, I created an admittedly basic TCP server that returns the uppercase version of any string sent out by the client.
Also, before I finish writing this section, I'd like you to notice that the script uses the "set_time_limit()" function, in order to get rid of any timeouts imposed by the PHP parser. This makes a lot of sense, since the script doesn't have a clue about when an eventual client is going to inject data into the socket, so eliminating timeouts is a good approach to keep the script working properly.
Now, the sample TCP server is up and running, listening to incoming client connections. Therefore, a client application should be created, in order to determine whether the sockets just created work as expected. That's exactly what I'll do in the next section, thus click on the link below to find out how this client is created.
blog comments powered by Disqus