uTools-Manuals/docs/php/socket_recvfrom.html
2019-04-28 19:00:34 +08:00

272 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Receives data from a socket whether or not it is connection-oriented</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.socket-recvfrom" class="refentry">
<div class="refnamediv">
<h1 class="refname">socket_recvfrom</h1>
<p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_recvfrom</span> &mdash; <span class="dc-title">Receives data from a socket whether or not it is connection-oriented</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.socket-recvfrom-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>socket_recvfrom</strong></span>
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$buf</code></span>
, <span class="methodparam"><span class="type">int</span> <code class="parameter">$len</code></span>
, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$name</code></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$port</code></span>
] ) : <span class="type">int</span></div>
<p class="para rdfs-comment">
The <span class="function"><strong>socket_recvfrom()</strong></span> function receives
<code class="parameter">len</code> bytes of data in <code class="parameter">buf</code> from
<code class="parameter">name</code> on port <code class="parameter">port</code> (if the
socket is not of type <strong><code>AF_UNIX</code></strong>) using
<code class="parameter">socket</code>. <span class="function"><strong>socket_recvfrom()</strong></span> 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 <code class="parameter">name</code> and <code class="parameter">port</code> must be
passed by reference. If the socket is not connection-oriented,
<code class="parameter">name</code> 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, <code class="parameter">name</code> is <strong><code>NULL</code></strong>. Additionally,
the <code class="parameter">port</code> will contain the port of the remote host in
the case of an unconnected <strong><code>AF_INET</code></strong> or
<strong><code>AF_INET6</code></strong> socket.
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.socket-recvfrom-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">socket</code></dt>
<dd>
<p class="para">
The <code class="parameter">socket</code> must be a socket resource previously
created by socket_create().
</p>
</dd>
<dt>
<code class="parameter">buf</code></dt>
<dd>
<p class="para">
The data received will be fetched to the variable specified with
<code class="parameter">buf</code>.
</p>
</dd>
<dt>
<code class="parameter">len</code></dt>
<dd>
<p class="para">
Up to <code class="parameter">len</code> bytes will be fetched from remote host.
</p>
</dd>
<dt>
<code class="parameter">flags</code></dt>
<dd>
<p class="para">
The value of <code class="parameter">flags</code> can be any combination of
the following flags, joined with the binary OR (<em>|</em>)
operator.
</p>
<table class="doctable table">
<caption><strong>Possible values for <code class="parameter">flags</code></strong></caption>
<thead>
<tr>
<th>Flag</th>
<th>Description</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td><strong><code>MSG_OOB</code></strong></td>
<td>
Process out-of-band data.
</td>
</tr>
<tr>
<td><strong><code>MSG_PEEK</code></strong></td>
<td>
Receive data from the beginning of the receive queue without
removing it from the queue.
</td>
</tr>
<tr>
<td><strong><code>MSG_WAITALL</code></strong></td>
<td>
Block until at least <code class="parameter">len</code> are received.
However, if a signal is caught or the remote host disconnects, the
function may return less data.
</td>
</tr>
<tr>
<td><strong><code>MSG_DONTWAIT</code></strong></td>
<td>
With this flag set, the function returns even if it would normally
have blocked.
</td>
</tr>
</tbody>
</table>
</dd>
<dt>
<code class="parameter">name</code></dt>
<dd>
<p class="para">
If the socket is of the type <strong><code>AF_UNIX</code></strong> type,
<code class="parameter">name</code> is the path to the file. Else, for
unconnected sockets, <code class="parameter">name</code> is the IP address of,
the remote host, or <strong><code>NULL</code></strong> if the socket is connection-oriented.
</p>
</dd>
<dt>
<code class="parameter">port</code></dt>
<dd>
<p class="para">
This argument only applies to <strong><code>AF_INET</code></strong> and
<strong><code>AF_INET6</code></strong> sockets, and specifies the remote port
from which the data is received. If the socket is connection-oriented,
<code class="parameter">port</code> will be <strong><code>NULL</code></strong>.
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.socket-recvfrom-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
<span class="function"><strong>socket_recvfrom()</strong></span> returns the number of bytes received,
or <strong><code>FALSE</code></strong> if there was an error. The actual error code can be retrieved by
calling <span class="function"><a href="socket_last_error.html" class="function">socket_last_error()</a></span>. This error code may be
passed to <span class="function"><a href="socket_strerror.html" class="function">socket_strerror()</a></span> to get a textual explanation
of the error.
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.socket-recvfrom-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="snmp.set.example.bits">
<p><strong>Example #1 <span class="function"><strong>socket_recvfrom()</strong></span> example</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">E_STRICT</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_DGRAM</span><span style="color: #007700">,&nbsp;</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">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1223</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$from&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$from</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Received&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #DD0000">&nbsp;from&nbsp;remote&nbsp;address&nbsp;</span><span style="color: #0000BB">$from</span><span style="color: #DD0000">&nbsp;and&nbsp;remote&nbsp;port&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>
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.
</p></div>
</div>
</p>
</div>
<div class="refsect1 changelog" id="refsect1-function.socket-recvfrom-changelog">
<h3 class="title">更新日志</h3>
<p class="para">
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>4.3.0</td>
<td>
<span class="function"><strong>socket_recvfrom()</strong></span> is now binary safe.
</td>
</tr>
</tbody>
</table>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.socket-recvfrom-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="socket_recv.html" class="function" rel="rdfs-seeAlso">socket_recv()</a> - 从已连接的socket接收数据</span></li>
<li class="member"><span class="function"><a href="socket_send.html" class="function" rel="rdfs-seeAlso">socket_send()</a> - Sends data to a connected socket</span></li>
<li class="member"><span class="function"><a href="socket_sendto.html" class="function" rel="rdfs-seeAlso">socket_sendto()</a> - Sends a message to a socket, whether it is connected or not</span></li>
<li class="member"><span class="function"><a href="socket_create.html" class="function" rel="rdfs-seeAlso">socket_create()</a> - 创建一个套接字(通讯节点)</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>