📄 function.socket-recvfrom.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Receives data from a socket whether or not it is connection-oriented</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-recv.html">socket_recv</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-select.html">socket_select</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.socket-recvfrom" class="refentry"> <div class="refnamediv"> <h1 class="refname">socket_recvfrom</h1> <p class="verinfo">(PHP 4 >= 4.0.7, PHP 5)</p><p class="refpurpose"><span class="refname">socket_recvfrom</span> — <span class="dc-title">Receives data from a socket whether or not it is connection-oriented</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>socket_recvfrom</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$socket</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter reference">&$buf</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$len</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$flags</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter reference">&$name</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter reference">&$port</tt></span> ] )</div> <p class="para rdfs-comment"> The <b>socket_recvfrom()</b> function receives <i><tt class="parameter">len</tt></i> bytes of data in <i><tt class="parameter">buf</tt></i> from <i><tt class="parameter">name</tt></i> on port <i><tt class="parameter">port</tt></i> (if the socket is not of type <b><tt>AF_UNIX</tt></b>) using <i><tt class="parameter">socket</tt></i>. <b>socket_recvfrom()</b> 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. </p> <p class="para"> The <i><tt class="parameter">name</tt></i> and <i><tt class="parameter">port</tt></i> must be passed by reference. If the socket is not connection-oriented, <i><tt class="parameter">name</tt></i> will be set to the internet protocol address of the remote host or the path to the UNIX socket. If the socket is connection-oriented, <i><tt class="parameter">name</tt></i> is <b><tt>NULL</tt></b>. Additionally, the <i><tt class="parameter">port</tt></i> will contain the port of the remote host in the case of an unconnected <b><tt>AF_INET</tt></b> or <b><tt>AF_INET6</tt></b> socket. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">socket</tt></i></span> <dd> <p class="para"> The <i><tt class="parameter">socket</tt></i> must be a socket resource previously created by socket_create(). </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">buf</tt></i></span> <dd> <p class="para"> The data received will be fetched to the variable specified with <i><tt class="parameter">buf</tt></i>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">len</tt></i></span> <dd> <p class="para"> Up to <i><tt class="parameter">len</tt></i> bytes will be fetched from remote host. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">flags</tt></i></span> <dd> <p class="para"> The value of <i><tt class="parameter">flags</tt></i> can be any combination of the following flags, joined with the binary OR (<i>|</i>) operator. </p> <table border="5"> <caption><b>Possible values for <i><tt class="parameter">flags</tt></i></b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Flag</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b><tt>MSG_OOB</tt></b></td> <td colspan="1" rowspan="1" align="left"> Process out-of-band data. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b><tt>MSG_PEEK</tt></b></td> <td colspan="1" rowspan="1" align="left"> Receive data from the beginning of the receive queue without removing it from the queue. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b><tt>MSG_WAITALL</tt></b></td> <td colspan="1" rowspan="1" align="left"> Block until at least <i><tt class="parameter">len</tt></i> are received. However, if a signal is caught or the remote host disconnects, the function may return less data. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b><tt>MSG_DONTWAIT</tt></b></td> <td colspan="1" rowspan="1" align="left"> With this flag set, the function returns even if it would normally have blocked. </td> </tr> </tbody> </colgroup> </table> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">name</tt></i></span> <dd> <p class="para"> If the socket is of the type <b><tt>AF_UNIX</tt></b> type, <i><tt class="parameter">name</tt></i> is the path to the file. Else, for unconnected sockets, <i><tt class="parameter">name</tt></i> is the IP address of, the remote host, or <b><tt>NULL</tt></b> if the socket is connection-oriented. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">port</tt></i></span> <dd> <p class="para"> This argument only applies to <b><tt>AF_INET</tt></b> and <b><tt>AF_INET6</tt></b> sockets, and specifies the remote port from which the data is received. If the socket is connection-oriented, <i><tt class="parameter">port</tt></i> will be <b><tt>NULL</tt></b>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <b>socket_recvfrom()</b> returns the number of bytes received, or -1 if there was an error. The actual error code can be retrieved by calling <a href="function.socket-last-error.html" class="function">socket_last_error()</a>. This error code may be passed to <a href="function.socket-strerror.html" class="function">socket_strerror()</a> to get a textual explanation of the error. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>socket_recvfrom()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL </span><span style="color: #007700">| </span><span style="color: #0000BB">E_STRICT</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$socket </span><span style="color: #007700">= </span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">, </span><span style="color: #0000BB">SOCK_DGRAM</span><span style="color: #007700">, </span><span style="color: #0000BB">SOL_UDP</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">socket_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">, </span><span style="color: #0000BB">1223</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$from </span><span style="color: #007700">= </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$port </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$buf</span><span style="color: #007700">, </span><span style="color: #0000BB">12</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$from</span><span style="color: #007700">, </span><span style="color: #0000BB">$port</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Received $buf from remote address $from and remote port $port" </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> This example will initiate an UDP socket on port 1223 of 127.0.0.1 and print at most 12 characters received from a remote host. </p></div> </div> </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> <b>socket_recvfrom()</b> is now binary safe. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.socket-recv.html" class="function" rel="rdfs-seeAlso">socket_recv()</a></li> <li class="member"><a href="function.socket-send.html" class="function" rel="rdfs-seeAlso">socket_send()</a></li> <li class="member"><a href="function.socket-sendto.html" class="function" rel="rdfs-seeAlso">socket_sendto()</a></li> <li class="member"><a href="function.socket-create.html" class="function" rel="rdfs-seeAlso">socket_create()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.socket-recv.html">socket_recv</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.socket-select.html">socket_select</a></div> <div class="up"><a href="ref.sockets.html">Socket Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -