I'm new to php socket programming and although I've trawled Google considerably, I can't seem to find a clear answer to the following problem. Please could you help?
I have a daemon whose inner loop simply reads in data from a socket and processes it.
Code: Select all
while ( !feof ( $sock ) )
{
$line = fgets ( $sock, 1024 );
echo $line;
sleep (0.1);
}Sometimes there will be many minutes between one line of the stream and the next. Therefore it is not suitable to use a blocking socket because if it's sent a SIGINT it may not respond to that signal for many minutes as it is waiting for a line of data.
However if I use a non-blocking socket, it uses 100% cpu, even if I put a sleep (0.1) into the loop.
I have a feeling that select() or socket_select() could help me here but all the documentation I can find seems to suggest that select() is only useful when you have multiple sockets and that it would not give me the non-blocking type of behaviour I require.
Thanks in advance!
A