socket_recv() - socket通信函数
socket_recv()
(PHP 4 >= 4.1.0, PHP 5, PHP 7)
从已连接的socket接收数据
说明
socket_recv(resource $socket,string &$buf,int $len,int $flags): int函数socket_recv()从$socket中接受长度为$len字节的数据,并保存在$buf中。socket_recv()用于从已连接的socket中接收数据。除此之外,可以设定一个或多个 flags 来控制函数的具体行为。
$buf以引用形式传递,因此必须是一个以声明的有效的变量。从$socket中接收到的数据将会保存在$buf中。
参数
$socket参数$socket必须是一个由socket_create()创建的socket资源。
$buf从socket中获取的数据将被保存在由$buf制定的变量中。如果有错误发生,如链接被重置,数据不可用等等,$buf将被设为NULL
。
长度最多为$len字节的数据将被接收。
$flags$flags的值可以为下列任意flag的组合。使用按位或运算符(|)来组合不同的flag。
Flag | 描述 |
---|---|
MSG_OOB | 处理超出边界的数据 |
MSG_PEEK | 从接受队列的起始位置接收数据,但不将他们从接受队列中移除。 |
MSG_WAITALL | 在接收到至少$len字节的数据之前,造成一个阻塞,并暂停脚本运行(block)。但是,如果接收到中断信号,或远程服务器断开连接,该函数将返回少于$len字节的数据。 |
MSG_DONTWAIT | 如果制定了该flag,函数将不会造成阻塞,即使在全局设置中指定了阻塞设置。 |
返回值
socket_recv()返回一个数字,表示接收到的字节数。如果发生了错误,则返回FALSE
错误码可使用socket_last_error()接收。也可使用函数socket_strerror()来取得关于错误的文字描述。
范例
socket_recv()范例
该范例简单地使用socket_recv()重写了范例中的第一个例子。
The above example will produce something like:
TCP/IP Connection
OK. Attempting to connect to '208.77.188.166' on port '80'...OK. Sending HTTP HEAD request...OK. Reading response: Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK Date: Mon, 14 Sep 2009 08:56:36 GMT Server: Apache/2.2.3 (Red Hat) Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT ETag: "b80f4-1b6-80bfd280" Accept-Ranges: bytes Content-Length: 438 Connection: close Content-Type: text/html; charset=UTF-8 OK.
I've used socket_select and socket_recv with a while loop and found myself in trouble when remote side closed connection. The code below produced infinite loop and socket_select returned immediately (which lead to high cpu time consumption). The solution was simple, but quite hard to find because socket_recv is not documented. socket_recv returns FALSE if there is no data and 0 if the socket is widowed (disconnected by remote side). So I had just to check return value of socket_recv. The problem now sounds stupid, but I've spend some time to find it out. I hope this will save some of somebody's hair ;)
Workaround for the missing MSG_DONTWAIT flag according to the bug report page:
In PHP version 5.* there is a bug: MSG_DONTWAIT flag is not defined (see https://bugs.php.net/bug.php?id=48326)
It looks like that mysterious flags are just the recv(2) flags passed to your OS syscall and nothing more... ext/sockets/sockets.c:PHP_FUNCTION(socket_recv) ... if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags))in case you want to empty/unset $buffer, but failing to do so, try using 0 as flag. PHP_NORMAL_READ and PHP_BINARY_READ respectively hold 1 and 2 as value.It seems like the flags are just passed to the underlying recv() function of your OS, hence there no MSG_DONTWAIT flag on Windows and you should not define it yourself in that case, it just won't work.My last post was incorrect. The int flag set to 2 apparently reset the file position pointer so what I was reading was the first record repeatedly. My workaroud ended up being the following: for($ct=1; $ct
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!