百科狗-知识改变命运!
--

socket_recvfrom() - socket通信函数

百变鹏仔12个月前 (11-21)阅读数 17#技术干货
文章标签节点

socket_recvfrom()

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

Receives data from a socket whether or not it is connection-oriented

说明

socket_recvfrom(resource $socket,string &$buf,int $len,int $flags,string &$name[,int &$port]): int

The socket_recvfrom() function receives$lenbytes of data in$buffrom$nameon port$port(if the socket is not of type AF_UNIX)using$socket.socket_recvfrom() can be used to gather data from both connected and unconnected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function.

The$nameand$portmust be passed by reference. If the socket is not connection-oriented,$namewill be set to the internet protocol address of the remote host or the path to the UNIX socket. If the socket is connection-oriented,$nameis NULL. Additionally, the$portwill contain the port of the remote host in the case of an unconnected AF_INET or AF_INET6 socket.

Note:此函数可安全用于二进制对象。

参数

$socket

socket_recvfrom() - socket通信函数

The$socketmust be a socket resource previously created by socket_create().

$buf

The data received will be fetched to the variable specified with$buf.

$len

Up to$lenbytes will be fetched from remote host.

$flags

The value of$flagscan be any combination of the following flags, joined with the binary OR(|)operator.

Possible values for$flags
FlagDescription
MSG_OOB Process out-of-band data.
MSG_PEEK Receive data from the beginning of the receive queue without removing it from the queue.
MSG_WAITALL Block until at least$lenare 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.
$name

If the socket is of the type AF_UNIX type,$nameis the path to the file. Else, for unconnected sockets,$nameis the IP address of, the remote host, or NULL if the socket is connection-oriented.

$port

This argument only applies to AF_INET and AF_INET6 sockets, and specifies the remote port from which the data is received. If the socket is connection-oriented,$portwill be NULL.

返回值

socket_recvfrom() returns the number of bytes received, or FALSE if there was an error. The actual error code can be retrieved by calling socket_last_error(). This error code may be passed to socket_strerror() to get a textual explanation of the error.

范例

socket_recvfrom() example

This example will initiate a UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host.

参见

  • socket_recv()从已连接的socket接收数据
  • socket_send()Sends data to a connected socket
  • socket_sendto()Sends a message to a socket, whether it is connected or not
  • socket_create()创建一个套接字(通讯节点)
If you use socket_recvfrom on a UDP socket and combine it with the MSG_DONTWAIT flag, it will raise a PHP Warning if there is nothing to read. AFAIK, there is no way around that warning except suppressing it with @ (i.e. you cannot check if there is data before calling socket_recvfrom).
Pay attention! On some PHP version the MSG_DONTWAIT flag is not defined (see https://bugs.php.net/bug.php?id=48326)
MSG_DONTWAIT doesn't seem to exist in windows sockets. However socket_set_nonblock() seems to do the trick.
I'm confused about the rerturn value of socket_recvfrom(), it said -1 when failed, but when I call like this:
if (($len = @socket_recvfrom($sock, $result, 32, 0, $ip, $port)) == -1) {
  if ($this->_debug) {
    echo "socket_read() failed: " . socket_strerror(socket_last_error()) . "\n";
  }
  return false;
}
variable $len = false, when I change the buffer length from 32 to 4096, it becomes right.
DNS RELAY USING UDP SOCKETS 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)