-
Notifications
You must be signed in to change notification settings - Fork 104
/
socket-select.xml
222 lines (214 loc) · 7.65 KB
/
socket-select.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 14dc7c47365f2b71f6c907a5ba5bccf42534d5a9 Maintainer: songmeifeng Status: ready -->
<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 套接字背景,会认识到这些套接字数组事实上被称为文件描述符集合。观察三个独立套接字数组。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>read</parameter></term>
<listitem>
<para>
<parameter>read</parameter> 数组中列出要检查是否可读的套接字(更准确地说,查看读取是否会阻塞-特别是套接字在 EOF 时也可以是就绪状态,此时 <function>socket_read</function>
将会返回长度为 0 的字符串。
</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>timeout</literal> 参数。<literal>timeout</literal>
是 <function>socket_select</function> 返回前的运行时间上限。<parameter>seconds</parameter>
可以是 0,使 <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 引擎的限制,无法使用像 &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>
返回数组中被修改的套接字个数,如果超时则可能返回 0。失败时时返回 &false;。可以使用 <function>socket_last_error</function>
检索错误码。
</para>
<note>
<para>
确保使用 <literal>===</literal> 运算符来检查错误。因为 <function>socket_select</function>
可能返回 0,使用 <literal>==</literal> 将被计算为 &true;:
<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() 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> 示例</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>
请注意,处理某些实现的套接字时需要特别小心。一些基本规则:
<itemizedlist>
<listitem>
<simpara>
始终使用不带超时时间的 <function>socket_select</function>,如果没有可用数据,程序不会做任何事。依赖超时的代码通常不可移植并且很难调试。
</simpara>
</listitem>
<listitem>
<simpara>
不打算在 <function>socket_select</function> 调用后检查值的套接字不要添加到集合中。<function>socket_select</function>
返回后,必须检查所有数组中的套接字。任何可写套接字需要处理写事件,任何可读套接字需要处理读事件。
</simpara>
</listitem>
<listitem>
<simpara>
在处理数组中返回套接字的读或写操作时,可能并不会读取或写入请求的所有数据。需要做好仅仅读取或写入单个字节的准备。
</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
-->