PHP Non-blocking Socket with Server-Client Communication: Part 1
Example Code
In PHP Blocking Socket, I introduced the concept of blocking socket. Now, I would unpack the 'unblocking socket'.
With unblocking sockets, we don't need to "wait" as in blocking sockets, also don't need to wait for the completion of the operation, and can connect the server with more than one client. the following is the example code:
server
client
Example Code Breakdown
Some code is explained in this article PHP Blocking Socket
socket_set_nonblock()
: set the socket to non-blocking mode, which makes the socket return immediately even if the connection or read has not been completed. Thus, this enables us to perform multiple socket connections.while..
,foreach
: create a loop to query through sockets to get a new connection and read messages from different clients.
However, foreach
is very inefficient, and I show a better way to do it in PHP non-blocking socket part 2