Socket Programming in PHP
There is a lot of tutorial information about socket programming in C, and most of it can be translated to PHP with slight modifications. For more details on this subject, the UNIX Sockets FAQ is a good resource to start with.
The first step in server-side socket programming is to create a socket using the socket_create() function. This will return a socket resource, or handle, which must be used in all subsequent function calls. The second step is to bind the socket to a specific name using the socket_bind() function, and tell it to listen for incoming connections with the socket_listen() function.
Finally, the server must process the input received from the client, which is done by reading it with the socket_read() function and assigning it to the $input variable. Once the input has been processed, it is sent back to the client via the socket_write() function.
During the connection process, the fgets() and fputs() functions may be used to read and write data, which will be inserted into or deleted from the connection buffer. When the process is complete, both sockets must be closed with the socket_close() function.
During this process, it is possible that the connecting operation will encounter errors. This can be determined by looking at the exit status (the value 115 is a typical one), or by getting a textual description of the error by calling socket_last_error(). Once the error has been resolved, the socket_connect() function will return a Boolean value indicating whether or not it succeeded.