/php/function.socket-recv.html
HTML | 227 lines | 181 code | 46 blank | 0 comment | 0 complexity | e8f45ed0e888495871835cc7c52c67d3 MD5 | raw file
- <!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 connected socket</title>
- </head>
- <body><div class="manualnavbar" style="text-align: center;">
- <div class="prev" style="text-align: left; float: left;"><a href="function.socket-read.html">socket_read</a></div>
- <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvfrom.html">socket_recvfrom</a></div>
- <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
- <div class="home"><a href="index.html">PHP Manual</a></div>
- </div><hr /><div id="function.socket-recv" class="refentry">
- <div class="refnamediv">
- <h1 class="refname">socket_recv</h1>
- <p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_recv</span> — <span class="dc-title">Receives data from a connected socket </span></p>
- </div>
-
- <div class="refsect1 description" id="refsect1-function.socket-recv-description">
- <h3 class="title">说明</h3>
- <div class="methodsynopsis dc-description">
- <span class="type">int</span> <span class="methodname"><strong>socket_recv</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>
- )</div>
- <p class="para rdfs-comment">
- The <span class="function"><strong>socket_recv()</strong></span> function receives
- <em><code class="parameter">len</code></em> bytes of data in <em><code class="parameter">buf</code></em> from
- <em><code class="parameter">socket</code></em>. <span class="function"><strong>socket_recv()</strong></span> can
- be used to gather data from connected sockets. Additionally, one or
- more flags can be specified to modify the behaviour of the
- function.
- </p>
- <p class="para">
- <em><code class="parameter">buf</code></em> is passed by reference, so it must be
- specified as a variable in the argument list. Data read from
- <em><code class="parameter">socket</code></em> by <span class="function"><strong>socket_recv()</strong></span>
- will be returned in <em><code class="parameter">buf</code></em>.
- </p>
- </div>
- <div class="refsect1 parameters" id="refsect1-function.socket-recv-parameters">
- <h3 class="title">参数</h3>
- <p class="para">
- <dl>
- <dt>
- <span class="term"><em><code class="parameter">socket</code></em></span>
- <dd>
- <p class="para">
- The <em><code class="parameter">socket</code></em> must be a socket resource previously
- created by socket_create().
- </p>
- </dd>
- </dt>
- <dt>
- <span class="term"><em><code class="parameter">buf</code></em></span>
- <dd>
- <p class="para">
- The data received will be fetched to the variable specified with
- <em><code class="parameter">buf</code></em>. If an error occurs, if the
- connection is reset, or if no data is
- available, <em><code class="parameter">buf</code></em> will be set to <strong><code>NULL</code></strong>.
- </p>
- </dd>
- </dt>
- <dt>
- <span class="term"><em><code class="parameter">len</code></em></span>
- <dd>
- <p class="para">
- Up to <em><code class="parameter">len</code></em> bytes will be fetched from remote host.
- </p>
- </dd>
- </dt>
- <dt>
- <span class="term"><em><code class="parameter">flags</code></em></span>
- <dd>
- <p class="para">
- The value of <em><code class="parameter">flags</code></em> 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 <em><code class="parameter">flags</code></em></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 <em><code class="parameter">len</code></em> 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>
-
- </dl>
- </p>
- </div>
- <div class="refsect1 returnvalues" id="refsect1-function.socket-recv-returnvalues">
- <h3 class="title">返回值</h3>
- <p class="para">
- <span class="function"><strong>socket_recv()</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="function.socket-last-error.html" class="function">socket_last_error()</a></span>. This error code may be
- passed to <span class="function"><a href="function.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-recv-examples">
- <h3 class="title">范例</h3>
- <p class="para">
- <div class="example" id="example-4360">
- <p><strong>Example #1 <span class="function"><strong>socket_recv()</strong></span> example</strong></p>
- <div class="example-contents"><p>
- This example is a simple rewrite of the first example from
- <a href="sockets.examples.html" class="xref">范例</a> to
- use <span class="function"><strong>socket_recv()</strong></span>.
- </p></div>
- <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">);<br /><br />echo </span><span style="color: #DD0000">"<h2>TCP/IP Connection</h2>\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Get the port for the WWW service. */<br /></span><span style="color: #0000BB">$service_port </span><span style="color: #007700">= </span><span style="color: #0000BB">getservbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www'</span><span style="color: #007700">, </span><span style="color: #DD0000">'tcp'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Get the IP address for the target host. */<br /></span><span style="color: #0000BB">$address </span><span style="color: #007700">= </span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Create a TCP/IP socket. */<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_STREAM</span><span style="color: #007700">, </span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$socket </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_create() failed: reason: " </span><span style="color: #007700">. </span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">()) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #DD0000">"Attempting to connect to '</span><span style="color: #0000BB">$address</span><span style="color: #DD0000">' on port '</span><span style="color: #0000BB">$service_port</span><span style="color: #DD0000">'..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$address</span><span style="color: #007700">, </span><span style="color: #0000BB">$service_port</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$result </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"socket_connect() failed.\nReason: (</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">) " </span><span style="color: #007700">. </span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$in </span><span style="color: #007700">= </span><span style="color: #DD0000">"HEAD / HTTP/1.1\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in </span><span style="color: #007700">.= </span><span style="color: #DD0000">"Host: www.example.com\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in </span><span style="color: #007700">.= </span><span style="color: #DD0000">"Connection: Close\r\n\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$out </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Sending HTTP HEAD request..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">, </span><span style="color: #0000BB">$in</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$in</span><span style="color: #007700">));<br />echo </span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #DD0000">"Reading response:\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$buf </span><span style="color: #007700">= </span><span style="color: #DD0000">'This is my buffer.'</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">false </span><span style="color: #007700">!== (</span><span style="color: #0000BB">$bytes </span><span style="color: #007700">= </span><span style="color: #0000BB">socket_recv</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">2048</span><span style="color: #007700">, </span><span style="color: #0000BB">MSG_WAITALL</span><span style="color: #007700">))) {<br /> echo </span><span style="color: #DD0000">"Read </span><span style="color: #0000BB">$bytes</span><span style="color: #DD0000"> bytes from socket_recv(). Closing socket..."</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"socket_recv() failed; reason: " </span><span style="color: #007700">. </span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$buf </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"OK.\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
- </span>
- </code></div>
- </div>
- <div class="example-contents"><p>
- The above example will produce something like:
- </p></div>
- <div class="example-contents screen">
- <div class="cdata"><pre>
- <h2>TCP/IP Connection</h2>
- OK.
- Attempting to connect to '208.77.188.166' on port '80'...OK.
- Sending HTTP HEAD request...OK.
- Reading response:
- Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK
- Date: Mon, 14 Sep 2009 08:56:36 GMT
- Server: Apache/2.2.3 (Red Hat)
- Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
- ETag: "b80f4-1b6-80bfd280"
- Accept-Ranges: bytes
- Content-Length: 438
- Connection: close
- Content-Type: text/html; charset=UTF-8
- OK.
- </pre></div>
- </div>
- </div>
- </p>
- </div>
-
- </div><hr /><div class="manualnavbar" style="text-align: center;">
- <div class="prev" style="text-align: left; float: left;"><a href="function.socket-read.html">socket_read</a></div>
- <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvfrom.html">socket_recvfrom</a></div>
- <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
- <div class="home"><a href="index.html">PHP Manual</a></div>
- </div></body></html>