PHP Non-blocking Socket: Part 2

In PHP Non-blocking Socket: Part 1, I use foreach to loop through sockets to get the message, but this is an inefficient way which consumes many resources of computers, there is a more elegant way to do this: socket_select.

Example Code

server

client

The client code is as same as the code in PHP Non-blocking Socket: Part 1

Example Code Breakdown

According to PHP manual, socket_select would run the select() system call on given arrays of sockets with a specified timeout, and wait for them to change status, and return 0 when none of the arrays changes, return 1 when it detects the status change in arrays. Then, I use socket_read() to see if the server gets a message from the client.