mirror of
https://github.com/php/doc-ja.git
synced 2026-03-23 22:52:11 +01:00
258 lines
9.9 KiB
XML
258 lines
9.9 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 14dc7c47365f2b71f6c907a5ba5bccf42534d5a9 Maintainer: hirokawa Status: ready -->
|
|
<!-- CREDITS: takagi -->
|
|
<refentry xml:id="function.socket-select" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>socket_select</refname>
|
|
<refpurpose>与えられたソケットの配列に対し、指定した有効時間で select() システムコールを実行する</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> はソケットの配列を受け取り、
|
|
それらの状態が変化するまで待ちます。BSD のソケットについての知識がある方なら、
|
|
これらのソケットの配列が、いわゆるファイル記述子セットであることがご理解いただけるでしょう。
|
|
3 つの独立した配列でソケットを監視します。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>read</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
配列 <parameter>read</parameter> に挙げられたソケットでは、
|
|
文字が読み込み可能になっているかどうか(厳密に言うと、読み込みが
|
|
ブロックされていないかどうか - 実際には、ソケット記述子はファイルの
|
|
終端でも有効です。そのような場合、<function>socket_read</function>
|
|
は長さゼロの文字列を返します)を監視します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>write</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
配列 <parameter>write</parameter> に挙げられたソケットでは、
|
|
書き込みがブロックされていないかどうかを監視します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>except</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
配列 <parameter>except</parameter> に挙げられたソケットでは、
|
|
例外を監視します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>seconds</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
<parameter>seconds</parameter> および <parameter>microseconds</parameter>
|
|
は、ともに<literal>タイムアウト</literal>を指定するパラメータです。
|
|
<literal>タイムアウト</literal>は、<function>socket_select</function>
|
|
が結果を返すまでの経過時間の最大値です。
|
|
<parameter>seconds</parameter> はゼロにすることも可能で、そうすると
|
|
<function>socket_select</function> は結果をすぐに返します。
|
|
これはポーリングをする際に有用です。<parameter>seconds</parameter>
|
|
に &null;(タイムアウトしない)を指定すると、
|
|
<function>socket_select</function> は無期限にブロックします。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>microseconds</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
<warning>
|
|
<para>
|
|
終了時に配列は書き換えられ、
|
|
どのソケットの状態が変わったのかがわかるようになります。
|
|
</para>
|
|
</warning>
|
|
<para>
|
|
<function>socket_select</function> のすべての配列を設定する必要はありません。
|
|
使用しないものについては空の配列や &null; をかわりに指定しておくことが可能です。
|
|
また、これらの配列は<emphasis>参照渡し</emphasis> であり、
|
|
<function>socket_select</function>
|
|
をコールした後でその中身が書き換えられていることに注意しましょう。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
現状の Zend Engine の制限により、関数の参照渡しパラメータに
|
|
&null; のような定数値を直接渡すことができません。一時的な変数を使用するか、
|
|
あるいは一番左に一時変数を使用する式を使用してください。
|
|
<example>
|
|
<title><function>socket_select</function> での &null; の使用</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>
|
|
成功した場合は、<function>socket_select</function> は配列内で
|
|
変化のあったソケットの数を返します。もし何かがおこる前に
|
|
タイムアウト時間が経過した場合は、ゼロを返すことになります。
|
|
エラー時には &false; が返されます。エラーコードは
|
|
<function>socket_last_error</function> で取得可能です。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
エラーかどうかを調べる際には、必ず <literal>===</literal> 演算子を
|
|
使用するようにしましょう。<function>socket_select</function> は
|
|
0 を返す場合もあり、このような場合に <literal>==</literal>
|
|
を用いて比較すると、エラーと判定されてしまいます。
|
|
<example>
|
|
<title><function>socket_select</function> の返す結果を知る</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$e = NULL;
|
|
if (false === socket_select($r, $w, $e, 0)) {
|
|
echo "socket_select() は失敗しました。原因: " .
|
|
socket_strerror(socket_last_error()) . "\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>socket_select</function> の例</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/* 読み込み用の配列を準備する */
|
|
$read = array($socket1, $socket2);
|
|
$write = NULL;
|
|
$except = NULL;
|
|
$num_changed_sockets = socket_select($read, $write, $except, 0);
|
|
|
|
if ($num_changed_sockets === false) {
|
|
/* エラー処理 */
|
|
} else if ($num_changed_sockets > 0) {
|
|
/* すくなくともひとつのソケットで、何らかの出来事が起こっています */
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
ソケットの実装によっては、取り扱いに注意すべきものがあることを知っておいてください。
|
|
基本的なルールは以下のとおりです。
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
基本的に <function>socket_select</function> のタイムアウトは
|
|
指定しないように心がけましょう。もしデータがなかった場合に、
|
|
プログラム側でそれを判定できなくなってしまいます。タイムアウトに
|
|
依存しているコードは移植性が悪く、デバッグが困難です。
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<function>socket_select</function> のコール後に値をチェックして
|
|
適切に処理するつもりがないソケットは、決して配列に追加してはいけません。
|
|
<function>socket_select</function> から値が返ってきたあとは、
|
|
配列内のすべてのソケットをチェックする必要があります。
|
|
すべての書き込み用ソケットは書き込める必要がありますし、
|
|
またすべての読み込み用ソケットは読み込める必要があります。
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
配列で返されたソケットに対して読み込み/書き込みをする場合には、
|
|
指定したデータを必ずしもすべて読み込み/書き込みするとは限らないことを
|
|
知っておいてください。たった 1 バイトしか読み込み/書き込みが
|
|
できなかった場合にも対処できるよう準備しておきましょう。
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
ほとんどのソケット実装で、<parameter>except</parameter> でキャッチできる
|
|
例外はただひとつ、すなわちソケットが受け取ったデータが帯域外で
|
|
あったということだけです。
|
|
</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
|
|
-->
|