mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-19 10:28:54 +00:00
255 lines
8.3 KiB
XML
255 lines
8.3 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<refentry xml:id="function.socket-select" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>socket_select</refname>
|
|
<refpurpose>Runs the select() system call on the given arrays of sockets with a specified timeout</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type class="union"><type>int</type><type>false</type></type><methodname>socket_select</methodname>
|
|
<methodparam><type class="union"><type>array</type><type>null</type></type><parameter role="reference">read</parameter></methodparam>
|
|
<methodparam><type class="union"><type>array</type><type>null</type></type><parameter role="reference">write</parameter></methodparam>
|
|
<methodparam><type class="union"><type>array</type><type>null</type></type><parameter role="reference">except</parameter></methodparam>
|
|
<methodparam><type class="union"><type>int</type><type>null</type></type><parameter>seconds</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>microseconds</parameter><initializer>0</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<function>socket_select</function> accepts arrays of sockets and waits for
|
|
them to change status. Those coming with BSD sockets background will
|
|
recognize that those socket arrays are in fact the so-called file
|
|
descriptor sets. Three independent arrays of sockets are watched.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>read</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The sockets listed in the <parameter>read</parameter> 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
|
|
is also ready on end-of-file, in which case a
|
|
<function>socket_read</function> will return a zero length string).
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>write</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The sockets listed in the <parameter>write</parameter> array will be
|
|
watched to see if a write will not block.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>except</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The sockets listed in the <parameter>except</parameter> array will be
|
|
watched for exceptions.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>seconds</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
The <parameter>seconds</parameter> and <parameter>microseconds</parameter>
|
|
together form the <literal>timeout</literal> parameter. The
|
|
<literal>timeout</literal> is an upper bound on the amount of time
|
|
elapsed before <function>socket_select</function> return.
|
|
<parameter>seconds</parameter> may be zero , causing
|
|
<function>socket_select</function> to return immediately. This is useful
|
|
for polling. If <parameter>seconds</parameter> is &null; (no timeout),
|
|
<function>socket_select</function> can block indefinitely.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>microseconds</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
<warning>
|
|
<para>
|
|
On exit, the arrays are modified to indicate which socket
|
|
actually changed status.
|
|
</para>
|
|
</warning>
|
|
<para>
|
|
You do not need to pass every array to
|
|
<function>socket_select</function>. You can leave it out and use an
|
|
empty array or &null; instead. Also do not forget that those arrays are
|
|
passed <emphasis>by reference</emphasis> and will be modified after
|
|
<function>socket_select</function> returns.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Due a limitation in the current Zend Engine it is not possible to pass a
|
|
constant modifier like &null; 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:
|
|
<example>
|
|
<title>Using &null; with <function>socket_select</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$e = NULL;
|
|
socket_select($r, $w, $e, 0);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
On success <function>socket_select</function> returns the number of
|
|
sockets contained in the modified arrays, which may be zero if
|
|
the timeout expires before anything interesting happens.On error &false;
|
|
is returned. The error code can be retrieved with
|
|
<function>socket_last_error</function>.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Be sure to use the <literal>===</literal> operator when checking for an
|
|
error. Since the <function>socket_select</function> may return 0 the
|
|
comparison with <literal>==</literal> would evaluate to &true;:
|
|
<example>
|
|
<title>Understanding <function>socket_select</function>'s result</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$e = NULL;
|
|
if (false === socket_select($r, $w, $e, 0)) {
|
|
echo "socket_select() failed, reason: " .
|
|
socket_strerror(socket_last_error()) . "\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>socket_select</function> example</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/* Prepare the read array */
|
|
$read = array($socket1, $socket2);
|
|
$write = NULL;
|
|
$except = NULL;
|
|
$num_changed_sockets = socket_select($read, $write, $except, 0);
|
|
|
|
if ($num_changed_sockets === false) {
|
|
/* Error handling */
|
|
} else if ($num_changed_sockets > 0) {
|
|
/* At least at one of the sockets something interesting happened */
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
Be aware that some socket implementations need to be handled very
|
|
carefully. A few basic rules:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
You should always try to use <function>socket_select</function>
|
|
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.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
No socket must be added to any set if you do not intend to
|
|
check its result after the <function>socket_select</function> call,
|
|
and respond appropriately. After <function>socket_select</function>
|
|
returns, all sockets in all arrays must be checked. Any
|
|
socket that is available for writing must be written to, and
|
|
any socket available for reading must be read from.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<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.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
It's common to most socket implementations that the only exception
|
|
caught with the <parameter>except</parameter> array is out-of-bound
|
|
data received on a socket.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>socket_read</function></member>
|
|
<member><function>socket_write</function></member>
|
|
<member><function>socket_last_error</function></member>
|
|
<member><function>socket_strerror</function></member>
|
|
</simplelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|