Php套接字服务器:读取所有输入

I'm experimenting with a very simple php server. It accepts one connection, reads what's sent, writes it and exits (I'm running it from the cli). The code is what follows. The problem is that whenever the length of the data exceeds 1024, it doesn't intercepts the end of data and so it just hangs. Any help? Thanks

$out= '';
while ($out = socket_read($client, 1024))
{
 echo $out; 
}

echo "ok";

socket_close($client);
socket_close($sock);

?>

Because your last socket_read will be hang on waiting for another data, why?

Because you while is never ending. socket_read function return data, data will be always, if will be not socket_read will be waiting for new data to result.

Use socket_select function to check data existence before you call socket_read then you will protect from the function hanging and in this moment you can end.

example ... Use socket_select function to check data existence before you call socket_read then you will protect from the function hanging and in this moment you can end.

please...