7

通过阅读 PHP 手册,socket_recv 和 socket_read 函数在我看来是一样的,这两个函数都从客户端获取数据。

谁能告诉我这两个功能有什么不同?

4

3 回答 3

15

socket_recv返回接收到的字节数 socket_read返回已经接收到的数据

您可以从缓冲区中读取字节并socket_recv知道已收到多少字节。socket_read您只能从缓冲区中读取特定数量的数据

于 2012-05-30T09:28:52.807 回答
2

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.

于 2012-05-30T09:33:46.580 回答
1
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.
于 2012-05-30T09:55:03.300 回答