mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
172 lines
9.5 KiB
HTML
172 lines
9.5 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, connected or not</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.stream-socket-recvfrom" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">stream_socket_recvfrom</h1>
|
|
<p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stream_socket_recvfrom</span> — <span class="dc-title">Receives data from a socket, connected or not </span></p>
|
|
|
|
</div>
|
|
<div class="refsect1 description" id="refsect1-function.stream-socket-recvfrom-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>stream_socket_recvfrom</strong></span>
|
|
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
|
|
, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
|
|
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>
|
|
[, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$address</code></span>
|
|
]] ) : <span class="type">string</span></div>
|
|
|
|
<p class="para rdfs-comment">
|
|
<span class="function"><strong>stream_socket_recvfrom()</strong></span> accepts
|
|
data from a remote socket up to <code class="parameter">length</code> bytes.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 parameters" id="refsect1-function.stream-socket-recvfrom-parameters">
|
|
<h3 class="title">参数</h3>
|
|
<p class="para">
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">socket</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The remote socket.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">length</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The number of bytes to receive from the <code class="parameter">socket</code>.
|
|
</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:
|
|
<table class="doctable table">
|
|
<caption><strong>Possible values for <code class="parameter">flags</code></strong></caption>
|
|
|
|
<tbody class="tbody">
|
|
<tr>
|
|
<td><strong><code>STREAM_OOB</code></strong></td>
|
|
<td>
|
|
Process OOB (<em>out-of-band</em>) data.
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td><strong><code>STREAM_PEEK</code></strong></td>
|
|
<td>
|
|
Retrieve data from the socket, but do not consume the buffer.
|
|
Subsequent calls to <span class="function"><a href="fread.html" class="function">fread()</a></span> or
|
|
<span class="function"><strong>stream_socket_recvfrom()</strong></span> will see
|
|
the same data.
|
|
</td>
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">address</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
If <code class="parameter">address</code> is provided it will be populated with
|
|
the address of the remote socket.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.stream-socket-recvfrom-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
Returns the read data, as a string
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.stream-socket-recvfrom-examples">
|
|
<h3 class="title">范例</h3>
|
|
<p class="para">
|
|
<div class="example" id="example-4840">
|
|
<p><strong>Example #1 <span class="function"><strong>stream_socket_recvfrom()</strong></span> example</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Open a server socket to port 1234 on localhost */<br /></span><span style="color: #0000BB">$server </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">'tcp://127.0.0.1:1234'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Accept a connection */<br /></span><span style="color: #0000BB">$socket </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_socket_accept</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Grab a packet (1500 is a typical MTU size) of OOB data */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Received Out-Of-Band: '" </span><span style="color: #007700">. </span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">1500</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_OOB</span><span style="color: #007700">) . </span><span style="color: #DD0000">"'\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Take a peek at the normal in-band data, but don't consume it. */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Data: '" </span><span style="color: #007700">. </span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">1500</span><span style="color: #007700">, </span><span style="color: #0000BB">STREAM_PEEK</span><span style="color: #007700">) . </span><span style="color: #DD0000">"'\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Get the exact same packet again, but remove it from the buffer this time. */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Data: '" </span><span style="color: #007700">. </span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">1500</span><span style="color: #007700">) . </span><span style="color: #DD0000">"'\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Close it up */<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 notes" id="refsect1-function.stream-socket-recvfrom-notes">
|
|
<h3 class="title">注释</h3>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
If a message received is longer than the <code class="parameter">length</code>
|
|
parameter, excess bytes may be discarded depending on the type of socket
|
|
the message is received from (such as UDP).
|
|
</p>
|
|
</p></blockquote>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
Calls to <span class="function"><strong>stream_socket_recvfrom()</strong></span> on socket-based
|
|
streams, after calls to buffer-based stream functions (like
|
|
<span class="function"><a href="fread.html" class="function">fread()</a></span> or <span class="function"><a href="stream_get_line.html" class="function">stream_get_line()</a></span>) read
|
|
data directly from the socket and bypass the stream buffer.
|
|
</p>
|
|
</p></blockquote>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 seealso" id="refsect1-function.stream-socket-recvfrom-seealso">
|
|
<h3 class="title">参见</h3>
|
|
<ul class="simplelist">
|
|
<li class="member"><span class="function"><a href="stream_socket_sendto.html" class="function" rel="rdfs-seeAlso">stream_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="stream_socket_client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
|
|
<li class="member"><span class="function"><a href="stream_socket_server.html" class="function" rel="rdfs-seeAlso">stream_socket_server()</a> - Create an Internet or Unix domain server socket</span></li>
|
|
</ul>
|
|
</div>
|
|
|
|
|
|
</div></div></div></body></html> |