mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 13:57:03 +08:00
255 lines
14 KiB
HTML
255 lines
14 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>Runs the select() system call on the given arrays of sockets with a specified timeout</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.socket-select" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">socket_select</h1>
|
|
<p class="verinfo">(PHP 4 >= 4.1.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_select</span> — <span class="dc-title">Runs the select() system call on the given arrays of sockets with a specified timeout</span></p>
|
|
|
|
</div>
|
|
|
|
<div class="refsect1 description" id="refsect1-function.socket-select-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>socket_select</strong></span>
|
|
( <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$read</code></span>
|
|
, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$write</code></span>
|
|
, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$except</code></span>
|
|
, <span class="methodparam"><span class="type">int</span> <code class="parameter">$tv_sec</code></span>
|
|
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$tv_usec</code><span class="initializer"> = 0</span></span>
|
|
] ) : <span class="type">int</span></div>
|
|
|
|
<p class="para rdfs-comment">
|
|
<span class="function"><strong>socket_select()</strong></span> accepts arrays of sockets and waits for
|
|
them to change status. Those coming with BSD sockets background will
|
|
recognize that those socket resource arrays are in fact the so-called file
|
|
descriptor sets. Three independent arrays of socket resources are watched.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 parameters" id="refsect1-function.socket-select-parameters">
|
|
<h3 class="title">参数</h3>
|
|
<p class="para">
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">read</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The sockets listed in the <code class="parameter">read</code> array will be
|
|
watched to see if characters become available for reading (more
|
|
precisely, to see if a read will not block - in particular, a socket
|
|
resource is also ready on end-of-file, in which case a
|
|
<span class="function"><a href="socket_read.html" class="function">socket_read()</a></span> will return a zero length string).
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">write</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The sockets listed in the <code class="parameter">write</code> array will be
|
|
watched to see if a write will not block.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">except</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The sockets listed in the <code class="parameter">except</code> array will be
|
|
watched for exceptions.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">tv_sec</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The <code class="parameter">tv_sec</code> and <code class="parameter">tv_usec</code>
|
|
together form the <em>timeout</em> parameter. The
|
|
<em>timeout</em> is an upper bound on the amount of time
|
|
elapsed before <span class="function"><strong>socket_select()</strong></span> return.
|
|
<code class="parameter">tv_sec</code> may be zero , causing
|
|
<span class="function"><strong>socket_select()</strong></span> to return immediately. This is useful
|
|
for polling. If <code class="parameter">tv_sec</code> is <strong><code>NULL</code></strong> (no timeout),
|
|
<span class="function"><strong>socket_select()</strong></span> can block indefinitely.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">tv_usec</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
<div class="warning"><strong class="warning">Warning</strong>
|
|
<p class="para">
|
|
On exit, the arrays are modified to indicate which socket resource
|
|
actually changed status.
|
|
</p>
|
|
</div>
|
|
<p class="para">
|
|
You do not need to pass every array to
|
|
<span class="function"><strong>socket_select()</strong></span>. You can leave it out and use an
|
|
empty array or <strong><code>NULL</code></strong> instead. Also do not forget that those arrays are
|
|
passed <em class="emphasis">by reference</em> and will be modified after
|
|
<span class="function"><strong>socket_select()</strong></span> returns.
|
|
</p>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
Due a limitation in the current Zend Engine it is not possible to pass a
|
|
constant modifier like <strong><code>NULL</code></strong> directly as a parameter to a function
|
|
which expects this parameter to be passed by reference. Instead use a
|
|
temporary variable or an expression with the leftmost member being a
|
|
temporary variable:
|
|
<div class="example" id="snmp.setsecurity.example.basic">
|
|
<p><strong>Example #1 Using <strong><code>NULL</code></strong> with <span class="function"><strong>socket_select()</strong></span></strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$e </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">, </span><span style="color: #0000BB">$w</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
</p>
|
|
</p></blockquote>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.socket-select-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
On success <span class="function"><strong>socket_select()</strong></span> returns the number of
|
|
socket resources contained in the modified arrays, which may be zero if
|
|
the timeout expires before anything interesting happens. On error <strong><code>FALSE</code></strong>
|
|
is returned. The error code can be retrieved with
|
|
<span class="function"><a href="socket_last_error.html" class="function">socket_last_error()</a></span>.
|
|
</p>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
Be sure to use the <em>===</em> operator when checking for an
|
|
error. Since the <span class="function"><strong>socket_select()</strong></span> may return 0 the
|
|
comparison with <em>==</em> would evaluate to <strong><code>TRUE</code></strong>:
|
|
<div class="example" id="snmp.walk.example.basic">
|
|
<p><strong>Example #2 Understanding <span class="function"><strong>socket_select()</strong></span>'s result</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$e </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">false </span><span style="color: #007700">=== </span><span style="color: #0000BB">socket_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">, </span><span style="color: #0000BB">$w</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"socket_select() failed, reason: " </span><span style="color: #007700">.<br /> </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 />}<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
</p>
|
|
</p></blockquote>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.socket-select-examples">
|
|
<h3 class="title">范例</h3>
|
|
<p class="para">
|
|
<div class="example" id="snmp.walk.example.suffix-as-key">
|
|
<p><strong>Example #3 <span class="function"><strong>socket_select()</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">/* Prepare the read array */<br /></span><span style="color: #0000BB">$read </span><span style="color: #007700">= array(</span><span style="color: #0000BB">$socket1</span><span style="color: #007700">, </span><span style="color: #0000BB">$socket2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$write </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$except </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$num_changed_sockets </span><span style="color: #007700">= </span><span style="color: #0000BB">socket_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$read</span><span style="color: #007700">, </span><span style="color: #0000BB">$write</span><span style="color: #007700">, </span><span style="color: #0000BB">$except</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$num_changed_sockets </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* Error handling */<br /></span><span style="color: #007700">} else if (</span><span style="color: #0000BB">$num_changed_sockets </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* At least at one of the sockets something interesting happened */<br /></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.socket-select-notes">
|
|
<h3 class="title">注释</h3>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
Be aware that some socket implementations need to be handled very
|
|
carefully. A few basic rules:
|
|
<ul class="itemizedlist">
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
You should always try to use <span class="function"><strong>socket_select()</strong></span>
|
|
without timeout. Your program should have nothing to do if there is
|
|
no data available. Code that depends on timeouts is not usually
|
|
portable and difficult to debug.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
No socket resource must be added to any set if you do not intend to
|
|
check its result after the <span class="function"><strong>socket_select()</strong></span> call,
|
|
and respond appropriately. After <span class="function"><strong>socket_select()</strong></span>
|
|
returns, all socket resources in all arrays must be checked. Any
|
|
socket resource that is available for writing must be written to, and
|
|
any socket resource available for reading must be read from.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
If you read/write to a socket returns in the arrays be aware that
|
|
they do not necessarily read/write the full amount of data you have
|
|
requested. Be prepared to even only be able to read/write a single
|
|
byte.
|
|
</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara">
|
|
It's common to most socket implementations that the only exception
|
|
caught with the <code class="parameter">except</code> array is out-of-bound
|
|
data received on a socket.
|
|
</span>
|
|
</li>
|
|
</ul>
|
|
</p>
|
|
</p></blockquote>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 seealso" id="refsect1-function.socket-select-seealso">
|
|
<h3 class="title">参见</h3>
|
|
<p class="para">
|
|
<ul class="simplelist">
|
|
<li class="member"><span class="function"><a href="socket_read.html" class="function" rel="rdfs-seeAlso">socket_read()</a> - Reads a maximum of length bytes from a socket</span></li>
|
|
<li class="member"><span class="function"><a href="socket_write.html" class="function" rel="rdfs-seeAlso">socket_write()</a> - Write to a socket</span></li>
|
|
<li class="member"><span class="function"><a href="socket_last_error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li>
|
|
<li class="member"><span class="function"><a href="socket_strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li>
|
|
</ul>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
</div></div></div></body></html> |