通过阅读 PHP 手册,socket_recv 和 socket_read 函数在我看来是一样的,这两个函数都从客户端获取数据。
谁能告诉我这两个功能有什么不同?
socket_recv
返回接收到的字节数
socket_read
返回已经接收到的数据
您可以从缓冲区中读取字节并socket_recv
知道已收到多少字节。socket_read
您只能从缓冲区中读取特定数量的数据
From http://www.faqs.org/faqs/unix-faq/socket/#b:
2.18. What is the difference between read() and recv()?
From Andrew Gierth (andrew@erlenstar.demon.co.uk):
read() is equivalent to recv() with a flags parameter of 0. Other
values for the flags parameter change the behaviour of recv().
Similarly, write() is equivalent to send() with flags == 0.
MSG_WAITALL Block until at least len are received. However, if a signal is caught or the remote host disconnects, the function may return less data.
MSG_DONTWAIT With this flag set, the function returns even if it would normally have blocked.
一种阻塞能力,它可以让函数等待直到接收到数据,当然通过使用 socket_recv,但是通过使用 socket_read,它假定字节已经接收并且不等待,所以它可能什么也不返回:
Note:
socket_read() returns a zero length string ("") when there is no more data to read.