socket_recvfrom() - socket通信函数
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:此函数可安全用于二进制对象。
参数
$socketThe$socketmust be a socket resource previously created by socket_create().
$bufThe data received will be fetched to the variable specified with$buf.
$lenUp to$lenbytes will be fetched from remote host.
$flagsThe value of$flagscan be any combination of the following flags, joined with the binary OR(|)operator.
Flag | Description |
---|---|
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. |
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.
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 socketsocket_sendto()
Sends a message to a socket, whether it is connected or notsocket_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
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!