Socket Programming in PHP
Socket programming has become one of the most important aspect of web development these days and its very easy to make real time applications. But to develop such applications we need to have a good knowledge of sockets and php language.
First thing we need to do is to create a socket, the function socket_create() does that for us. After creating the socket we need to bind it to an address and port that can be used by other clients to communicate with each other, this is done by the function socket_bind().
Now we are ready to start sending and receiving data from other clients. To send data we need to use the function socket_write(), to receive data from other client we need to use the function socket_read().
Whenever any of the above functions fails it will return an error code, you can find this error message using the function socket_strerror(), it takes as argument errno which is the socket error number and returns a string that explains it. You can also use socket_clear_error() to clear all the errors from the socket.
Almost all the socket functions in php will emit an E_WARNING message which explains the error, but sometimes this doesn't happen to the desire of the developer for example the function socket_read() may suddenly break unexpectedly. In those cases it is common to suppress the E_WARNING message and catch the error codes with the socket_last_error and socket_strerror functions and handle it accordingly.