PHP: Sockets, socket_read() only returns 1st character
I'm new to socket programming, I'm just starting to play around with a multi user server script. The problem comes with reading a socket, if my response is
"A, some开发者_运维百科thing or other"
socket_read only gives me "A"
Any idea why this may be occuring?
if (false === ($input = socket_read($clients[$i], 2048, PHP_BINARY_READ))) {
Source: http://pastebin.com/dEvYLUfV
If I'm not mistaken, socket_read only reads what data is currently available, and perhaps your PHP is running faster than your server socket (and is trying to read the data before it's available).
What you should do is add some kind of header to your message, which indicates the length of the expected message. Then, you keep "socket_read"ing until you've read that many bytes.
精彩评论