reference/sockets/functions/socket-recv.xml
14dc7c47365f2b71f6c907a5ba5bccf42534d5a9
...
...
@@ -1,6 +1,6 @@
1
-
<?xml version="1.0" encoding="iso-8859-1"?>
1
+
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.socket-recv">
3
+
<refentry xml:id="function.socket-recv" xmlns="http://docbook.org/ns/docbook">
4
4
<refnamediv>
5
5
<refname>socket_recv</refname>
6
6
<refpurpose>Receives data from a connected socket </refpurpose>
...
...
@@ -9,19 +9,238 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>int</type><methodname>socket_recv</methodname>
13
-
<methodparam><type>resource</type><parameter>socket</parameter></methodparam>
14
-
<methodparam><type>string</type><parameter role="reference">buf</parameter></methodparam>
15
-
<methodparam><type>int</type><parameter>len</parameter></methodparam>
12
+
<type class="union"><type>int</type><type>false</type></type><methodname>socket_recv</methodname>
13
+
<methodparam><type>Socket</type><parameter>socket</parameter></methodparam>
14
+
<methodparam><type class="union"><type>string</type><type>null</type></type><parameter role="reference">data</parameter></methodparam>
15
+
<methodparam><type>int</type><parameter>length</parameter></methodparam>
16
16
<methodparam><type>int</type><parameter>flags</parameter></methodparam>
17
17
</methodsynopsis>
18
18
<para>
19
-
&warn.undocumented.func;
19
+
The <function>socket_recv</function> function receives
20
+
<parameter>length</parameter> bytes of data in <parameter>data</parameter> from
21
+
<parameter>socket</parameter>. <function>socket_recv</function> can
22
+
be used to gather data from connected sockets. Additionally, one or
23
+
more flags can be specified to modify the behaviour of the
24
+
function.
25
+
</para>
26
+
<para>
27
+
<parameter>data</parameter> is passed by reference, so it must be
28
+
specified as a variable in the argument list. Data read from
29
+
<parameter>socket</parameter> by <function>socket_recv</function>
30
+
will be returned in <parameter>data</parameter>.
20
31
</para>
21
32
</refsect1>
22
33

23
-
</refentry>
34
+
<refsect1 role="parameters">
35
+
&reftitle.parameters;
36
+
<para>
37
+
<variablelist>
38
+
<varlistentry>
39
+
<term><parameter>socket</parameter></term>
40
+
<listitem>
41
+
<para>
42
+
The <parameter>socket</parameter> must be a <classname>Socket</classname> instance previously
43
+
created by socket_create().
44
+
</para>
45
+
</listitem>
46
+
</varlistentry>
47
+

48
+
<varlistentry>
49
+
<term><parameter>data</parameter></term>
50
+
<listitem>
51
+
<para>
52
+
The data received will be fetched to the variable specified with
53
+
<parameter>data</parameter>. If an error occurs, if the
54
+
connection is reset, or if no data is
55
+
available, <parameter>data</parameter> will be set to &null;.
56
+
</para>
57
+
</listitem>
58
+
</varlistentry>
59
+

60
+
<varlistentry>
61
+
<term><parameter>length</parameter></term>
62
+
<listitem>
63
+
<para>
64
+
Up to <parameter>length</parameter> bytes will be fetched from remote host.
65
+
</para>
66
+
</listitem>
67
+
</varlistentry>
68
+

69
+
<varlistentry>
70
+
<term><parameter>flags</parameter></term>
71
+
<listitem>
72
+
<para>
73
+
The value of <parameter>flags</parameter> can be any combination of
74
+
the following flags, joined with the binary OR (<literal>|</literal>)
75
+
operator.
76
+
</para>
77
+
78
+
<table>
79
+
<title>Possible values for <parameter>flags</parameter></title>
80
+
<tgroup cols="2">
81
+
<thead>
82
+
<row>
83
+
<entry>Flag</entry>
84
+
<entry>Description</entry>
85
+
</row>
86
+
</thead>
87
+
<tbody>
88
+
<row>
89
+
<entry><constant>MSG_OOB</constant></entry>
90
+
<entry>
91
+
Process out-of-band data.
92
+
</entry>
93
+
</row>
94
+
<row>
95
+
<entry><constant>MSG_PEEK</constant></entry>
96
+
<entry>
97
+
Receive data from the beginning of the receive queue without
98
+
removing it from the queue.
99
+
</entry>
100
+
</row>
101
+
<row>
102
+
<entry><constant>MSG_WAITALL</constant></entry>
103
+
<entry>
104
+
Block until at least <parameter>length</parameter> are received.
105
+
However, if a signal is caught or the remote host disconnects, the
106
+
function may return less data.
107
+
</entry>
108
+
</row>
109
+
<row>
110
+
<entry><constant>MSG_DONTWAIT</constant></entry>
111
+
<entry>
112
+
With this flag set, the function returns even if it would normally
113
+
have blocked.
114
+
</entry>
115
+
</row>
116
+
</tbody>
117
+
</tgroup>
118
+
</table>
119
+
</listitem>
120
+
</varlistentry>
121
+
</variablelist>
122
+
</para>
123
+
</refsect1>
124
+

125
+
<refsect1 role="returnvalues">
126
+
&reftitle.returnvalues;
127
+
<para>
128
+
<function>socket_recv</function> returns the number of bytes received,
129
+
or &false; if there was an error. The actual error code can be retrieved by
130
+
calling <function>socket_last_error</function>. This error code may be
131
+
passed to <function>socket_strerror</function> to get a textual explanation
132
+
of the error.
133
+
</para>
134
+
</refsect1>
135
+

136
+
<refsect1 role="changelog">
137
+
&reftitle.changelog;
138
+
<informaltable>
139
+
<tgroup cols="2">
140
+
<thead>
141
+
<row>
142
+
<entry>&Version;</entry>
143
+
<entry>&Description;</entry>
144
+
</row>
145
+
</thead>
146
+
<tbody>
147
+
&sockets.changelog.socket-param;
148
+
</tbody>
149
+
</tgroup>
150
+
</informaltable>
151
+
</refsect1>
24
152

153
+
<refsect1 role="examples">
154
+
&reftitle.examples;
155
+
<para>
156
+
<example>
157
+
<title><function>socket_recv</function> example</title>
158
+
<para>
159
+
This example is a simple rewrite of the first example from
160
+
<xref linkend="sockets.examples"/> to
161
+
use <function>socket_recv</function>.
162
+
</para>
163
+
<programlisting role="php">
164
+
<![CDATA[
165
+
<?php
166
+
error_reporting(E_ALL);
167
+

168
+
echo "<h2>TCP/IP Connection</h2>\n";
169
+

170
+
/* Get the port for the WWW service. */
171
+
$service_port = getservbyname('www', 'tcp');
172
+

173
+
/* Get the IP address for the target host. */
174
+
$address = gethostbyname('www.example.com');
175
+

176
+
/* Create a TCP/IP socket. */
177
+
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
178
+
if ($socket === false) {
179
+
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
180
+
} else {
181
+
echo "OK.\n";
182
+
}
183
+

184
+
echo "Attempting to connect to '$address' on port '$service_port'...";
185
+
$result = socket_connect($socket, $address, $service_port);
186
+
if ($result === false) {
187
+
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
188
+
} else {
189
+
echo "OK.\n";
190
+
}
191
+

192
+
$in = "HEAD / HTTP/1.1\r\n";
193
+
$in .= "Host: www.example.com\r\n";
194
+
$in .= "Connection: Close\r\n\r\n";
195
+
$out = '';
196
+

197
+
echo "Sending HTTP HEAD request...";
198
+
socket_write($socket, $in, strlen($in));
199
+
echo "OK.\n";
200
+

201
+
echo "Reading response:\n\n";
202
+
$buf = 'This is my buffer.';
203
+
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
204
+
echo "Read $bytes bytes from socket_recv(). Closing socket...";
205
+
} else {
206
+
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
207
+
}
208
+
socket_close($socket);
209
+

210
+
echo $buf . "\n";
211
+
echo "OK.\n\n";
212
+
?>
213
+
]]>
214
+
</programlisting>
215
+
<para>
216
+
The above example will produce something like:
217
+
</para>
218
+
<screen>
219
+
<![CDATA[
220
+
<h2>TCP/IP Connection</h2>
221
+
OK.
222
+
Attempting to connect to '208.77.188.166' on port '80'...OK.
223
+
Sending HTTP HEAD request...OK.
224
+
Reading response:
225
+

226
+
Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK
227
+
Date: Mon, 14 Sep 2009 08:56:36 GMT
228
+
Server: Apache/2.2.3 (Red Hat)
229
+
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
230
+
ETag: "b80f4-1b6-80bfd280"
231
+
Accept-Ranges: bytes
232
+
Content-Length: 438
233
+
Connection: close
234
+
Content-Type: text/html; charset=UTF-8
235
+

236
+
OK.
237
+
]]>
238
+
</screen>
239
+
</example>
240
+
</para>
241
+
</refsect1>
242
+
243
+
</refentry>
25
244
<!-- Keep this comment at the end of the file
26
245
Local variables:
27
246
mode: sgml
...
...
@@ -33,7 +252,7 @@ sgml-indent-step:1
33
252
sgml-indent-data:t
34
253
indent-tabs-mode:nil
35
254
sgml-parent-document:nil
36
-
sgml-default-dtd-file:"../../../../manual.ced"
255
+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
37
256
sgml-exposed-tags:nil
38
257
sgml-local-catalogs:nil
39
258
sgml-local-ecat-files:nil
40
259