PageRenderTime 35ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php/function.socket-recv.html

https://bitbucket.org/thncr/manuals
HTML | 227 lines | 181 code | 46 blank | 0 comment | 0 complexity | e8f45ed0e888495871835cc7c52c67d3 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <title>Receives data from a connected socket</title>
  6. </head>
  7. <body><div class="manualnavbar" style="text-align: center;">
  8. <div class="prev" style="text-align: left; float: left;"><a href="function.socket-read.html">socket_read</a></div>
  9. <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvfrom.html">socket_recvfrom</a></div>
  10. <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
  11. <div class="home"><a href="index.html">PHP Manual</a></div>
  12. </div><hr /><div id="function.socket-recv" class="refentry">
  13. <div class="refnamediv">
  14. <h1 class="refname">socket_recv</h1>
  15. <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_recv</span> &mdash; <span class="dc-title">Receives data from a connected socket </span></p>
  16. </div>
  17. <div class="refsect1 description" id="refsect1-function.socket-recv-description">
  18. <h3 class="title">说明</h3>
  19. <div class="methodsynopsis dc-description">
  20. <span class="type">int</span> <span class="methodname"><strong>socket_recv</strong></span>
  21. ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
  22. , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$buf</code></span>
  23. , <span class="methodparam"><span class="type">int</span> <code class="parameter">$len</code></span>
  24. , <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
  25. )</div>
  26. <p class="para rdfs-comment">
  27. The <span class="function"><strong>socket_recv()</strong></span> function receives
  28. <em><code class="parameter">len</code></em> bytes of data in <em><code class="parameter">buf</code></em> from
  29. <em><code class="parameter">socket</code></em>. <span class="function"><strong>socket_recv()</strong></span> can
  30. be used to gather data from connected sockets. Additionally, one or
  31. more flags can be specified to modify the behaviour of the
  32. function.
  33. </p>
  34. <p class="para">
  35. <em><code class="parameter">buf</code></em> is passed by reference, so it must be
  36. specified as a variable in the argument list. Data read from
  37. <em><code class="parameter">socket</code></em> by <span class="function"><strong>socket_recv()</strong></span>
  38. will be returned in <em><code class="parameter">buf</code></em>.
  39. </p>
  40. </div>
  41. <div class="refsect1 parameters" id="refsect1-function.socket-recv-parameters">
  42. <h3 class="title">参数</h3>
  43. <p class="para">
  44. <dl>
  45. <dt>
  46. <span class="term"><em><code class="parameter">socket</code></em></span>
  47. <dd>
  48. <p class="para">
  49. The <em><code class="parameter">socket</code></em> must be a socket resource previously
  50. created by socket_create().
  51. </p>
  52. </dd>
  53. </dt>
  54. <dt>
  55. <span class="term"><em><code class="parameter">buf</code></em></span>
  56. <dd>
  57. <p class="para">
  58. The data received will be fetched to the variable specified with
  59. <em><code class="parameter">buf</code></em>. If an error occurs, if the
  60. connection is reset, or if no data is
  61. available, <em><code class="parameter">buf</code></em> will be set to <strong><code>NULL</code></strong>.
  62. </p>
  63. </dd>
  64. </dt>
  65. <dt>
  66. <span class="term"><em><code class="parameter">len</code></em></span>
  67. <dd>
  68. <p class="para">
  69. Up to <em><code class="parameter">len</code></em> bytes will be fetched from remote host.
  70. </p>
  71. </dd>
  72. </dt>
  73. <dt>
  74. <span class="term"><em><code class="parameter">flags</code></em></span>
  75. <dd>
  76. <p class="para">
  77. The value of <em><code class="parameter">flags</code></em> can be any combination of
  78. the following flags, joined with the binary OR (<em>|</em>)
  79. operator.
  80. </p>
  81. <table class="doctable table">
  82. <caption><strong>Possible values for <em><code class="parameter">flags</code></em></strong></caption>
  83. <thead>
  84. <tr>
  85. <th>Flag</th>
  86. <th>Description</th>
  87. </tr>
  88. </thead>
  89. <tbody class="tbody">
  90. <tr>
  91. <td><strong><code>MSG_OOB</code></strong></td>
  92. <td>
  93. Process out-of-band data.
  94. </td>
  95. </tr>
  96. <tr>
  97. <td><strong><code>MSG_PEEK</code></strong></td>
  98. <td>
  99. Receive data from the beginning of the receive queue without
  100. removing it from the queue.
  101. </td>
  102. </tr>
  103. <tr>
  104. <td><strong><code>MSG_WAITALL</code></strong></td>
  105. <td>
  106. Block until at least <em><code class="parameter">len</code></em> are received.
  107. However, if a signal is caught or the remote host disconnects, the
  108. function may return less data.
  109. </td>
  110. </tr>
  111. <tr>
  112. <td><strong><code>MSG_DONTWAIT</code></strong></td>
  113. <td>
  114. With this flag set, the function returns even if it would normally
  115. have blocked.
  116. </td>
  117. </tr>
  118. </tbody>
  119. </table>
  120. </dd>
  121. </dt>
  122. </dl>
  123. </p>
  124. </div>
  125. <div class="refsect1 returnvalues" id="refsect1-function.socket-recv-returnvalues">
  126. <h3 class="title">返回值</h3>
  127. <p class="para">
  128. <span class="function"><strong>socket_recv()</strong></span> returns the number of bytes received,
  129. or <strong><code>FALSE</code></strong> if there was an error. The actual error code can be retrieved by
  130. calling <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span>. This error code may be
  131. passed to <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span> to get a textual explanation
  132. of the error.
  133. </p>
  134. </div>
  135. <div class="refsect1 examples" id="refsect1-function.socket-recv-examples">
  136. <h3 class="title">范例</h3>
  137. <p class="para">
  138. <div class="example" id="example-4360">
  139. <p><strong>Example #1 <span class="function"><strong>socket_recv()</strong></span> example</strong></p>
  140. <div class="example-contents"><p>
  141. This example is a simple rewrite of the first example from
  142. <a href="sockets.examples.html" class="xref">范例</a> to
  143. use <span class="function"><strong>socket_recv()</strong></span>.
  144. </p></div>
  145. <div class="example-contents">
  146. <div class="phpcode"><code><span style="color: #000000">
  147. <span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h2&gt;TCP/IP&nbsp;Connection&lt;/h2&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;port&nbsp;for&nbsp;the&nbsp;WWW&nbsp;service.&nbsp;*/<br /></span><span style="color: #0000BB">$service_port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getservbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tcp'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;IP&nbsp;address&nbsp;for&nbsp;the&nbsp;target&nbsp;host.&nbsp;*/<br /></span><span style="color: #0000BB">$address&nbsp;</span><span style="color: #007700">=&nbsp;</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">/*&nbsp;Create&nbsp;a&nbsp;TCP/IP&nbsp;socket.&nbsp;*/<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_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_create()&nbsp;failed:&nbsp;reason:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">())&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Attempting&nbsp;to&nbsp;connect&nbsp;to&nbsp;'</span><span style="color: #0000BB">$address</span><span style="color: #DD0000">'&nbsp;on&nbsp;port&nbsp;'</span><span style="color: #0000BB">$service_port</span><span style="color: #DD0000">'..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$address</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$service_port</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_connect()&nbsp;failed.\nReason:&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">)&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"HEAD&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;www.example.com\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Sending&nbsp;HTTP&nbsp;HEAD&nbsp;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">,&nbsp;</span><span style="color: #0000BB">$in</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$in</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Reading&nbsp;response:\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;my&nbsp;buffer.'</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">!==&nbsp;(</span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_recv</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">2048</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MSG_WAITALL</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Read&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #DD0000">&nbsp;bytes&nbsp;from&nbsp;socket_recv().&nbsp;Closing&nbsp;socket..."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_recv()&nbsp;failed;&nbsp;reason:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</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">))&nbsp;.&nbsp;</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&nbsp;</span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"OK.\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
  148. </span>
  149. </code></div>
  150. </div>
  151. <div class="example-contents"><p>
  152. The above example will produce something like:
  153. </p></div>
  154. <div class="example-contents screen">
  155. <div class="cdata"><pre>
  156. &lt;h2&gt;TCP/IP Connection&lt;/h2&gt;
  157. OK.
  158. Attempting to connect to &#039;208.77.188.166&#039; on port &#039;80&#039;...OK.
  159. Sending HTTP HEAD request...OK.
  160. Reading response:
  161. Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK
  162. Date: Mon, 14 Sep 2009 08:56:36 GMT
  163. Server: Apache/2.2.3 (Red Hat)
  164. Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
  165. ETag: &quot;b80f4-1b6-80bfd280&quot;
  166. Accept-Ranges: bytes
  167. Content-Length: 438
  168. Connection: close
  169. Content-Type: text/html; charset=UTF-8
  170. OK.
  171. </pre></div>
  172. </div>
  173. </div>
  174. </p>
  175. </div>
  176. </div><hr /><div class="manualnavbar" style="text-align: center;">
  177. <div class="prev" style="text-align: left; float: left;"><a href="function.socket-read.html">socket_read</a></div>
  178. <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvfrom.html">socket_recvfrom</a></div>
  179. <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
  180. <div class="home"><a href="index.html">PHP Manual</a></div>
  181. </div></body></html>