Basically, I’m using a blocking TCP socket along with socket_accept(). The problem I have, is that for obvious reasons since the I/O is blocking there can only be one connection to the socket at any one time that PHP is able to manipulate. I am fine with this, as this is the behaviour I am after.
However, if a client is to open a connection to the my PHP server, if they are simply leaving the socket open and sending no data, the socket and daemon are rendered useless as the blocking I/O will not allow new requests.
Is there a way to detect a client that is simply sending no data (like a no-transmission timeout) or something similar? I’d rather not use non-blocking I/O.
You can simply call the equivalent of
stream_set_timeoutto make the blocking reads and writes terminate after a certain timeout:Note that this is a hack; a malicious attacker can still send only 1 byte per second or so.
Instead of investing time in these crazy hacks, you should really switch to non-blocking IO or handle the accepted sockets in a separate thread/process.