PHP:套接字,socket_read() 仅返回第一个字符
我是套接字编程的新手,我刚刚开始使用多用户服务器脚本。问题在于读取套接字,如果我的响应是
“A,某事或其他”,
socket_read 只给我“A”
知道为什么会发生这种情况吗?
if (false === ($input = socket_read($clients[$i], 2048, PHP_BINARY_READ))) {
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, something 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,socket_read 只读取当前可用的数据,也许您的 PHP 运行速度比服务器套接字快(并且尝试在数据可用之前读取数据)。
您应该做的是在消息中添加某种标头,该标头指示预期消息的长度。然后,继续“socket_read”,直到读取了那么多字节。
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.