-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathsocket-recv.xml
More file actions
261 lines (240 loc) · 7.67 KB
/
socket-recv.xml
File metadata and controls
261 lines (240 loc) · 7.67 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 765056369ba5cfb582b2edfd9d83831b70690feb Maintainer: PhilDaiguille Status: ready -->
<!-- Reviewed: no -->
<refentry xml:id="function.socket-recv" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>socket_recv</refname>
<refpurpose>Recibe datos de un socket conectado</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>int</type><type>false</type></type><methodname>socket_recv</methodname>
<methodparam><type>Socket</type><parameter>socket</parameter></methodparam>
<methodparam><type class="union"><type>string</type><type>null</type></type><parameter role="reference">data</parameter></methodparam>
<methodparam><type>int</type><parameter>length</parameter></methodparam>
<methodparam><type>int</type><parameter>flags</parameter></methodparam>
</methodsynopsis>
<para>
La función <function>socket_recv</function> recibe
<parameter>length</parameter> bytes de datos en <parameter>data</parameter> desde
<parameter>socket</parameter>. <function>socket_recv</function> puede ser utilizada
para recuperar datos desde sockets conectados. Asimismo, los flags
permiten modificar el comportamiento de la función.
</para>
<para>
<parameter>data</parameter> se pasa por referencia, por lo que debe estar presente
en la lista de argumentos. Los datos recibidos desde la
<parameter>socket</parameter> por <function>socket_recv</function>
serán devueltos en <parameter>data</parameter>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>socket</parameter></term>
<listitem>
<para>
<parameter>socket</parameter> debe ser una instancia de <classname>Socket</classname>
previamente creada por <function>socket_create</function>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>data</parameter></term>
<listitem>
<para>
Los datos recibidos serán transmitidos en
<parameter>data</parameter>. Si ocurre un error, si la conexión
está cerrada o si no hay datos disponibles,
<parameter>data</parameter> valdrá entonces &null;.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>length</parameter></term>
<listitem>
<para>
Hasta <parameter>length</parameter> bytes serán leídos desde
el host remoto.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
El valor de <parameter>flags</parameter> puede ser una combinación
de los siguientes flags, agrupados mediante el operador OR binario
(<literal>|</literal>).
</para>
<table>
<title>Valores posibles de <parameter>flags</parameter></title>
<tgroup cols="2">
<thead>
<row>
<entry>Flag</entry>
<entry>Descripción</entry>
</row>
</thead>
<tbody>
<row>
<entry><constant>MSG_OOB</constant></entry>
<entry>
Trata datos fuera de banda.
</entry>
</row>
<row>
<entry><constant>MSG_PEEK</constant></entry>
<entry>
Recibe los datos desde el inicio de la cola sin eliminarlos de la cola.
</entry>
</row>
<row>
<entry><constant>MSG_WAITALL</constant></entry>
<entry>
Bloquea antes de que al menos <parameter>length</parameter> bytes sean recibidos.
Sin embargo, si se intercepta una señal o la conexión se termina,
la función puede devolver menos datos.
</entry>
</row>
<row>
<entry><constant>MSG_DONTWAIT</constant></entry>
<entry>
Si este flag está especificado, la función devolverá datos cuando normalmente habría bloqueado.
</entry>
</row>
</tbody>
</tgroup>
</table>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>socket_recv</function> devuelve el número de bytes recibidos,
o &false; si ha ocurrido un error. El código de error actual puede ser
recuperado llamando a <function>socket_last_error</function>. Este código
de error puede ser pasado a la función <function>socket_strerror</function>
para obtener una representación textual del error.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
&sockets.changelog.socket-param;
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Ejemplos con <function>socket_recv</function></title>
<para>
Este ejemplo es una reescritura del ejemplo tomado en
<xref linkend="sockets.examples"/> para utilizar
<function>socket_recv</function>.
</para>
<programlisting role="php">
<![CDATA[
<?php
error_reporting(E_ALL);
echo "<h2>Conexión TCP/IP</h2>\n";
/* Obtiene el puerto del servicio WWW. */
$service_port = getservbyname('www', 'tcp');
/* Obtiene la dirección IP del host objetivo. */
$address = gethostbyname('www.example.com');
/* Crea un socket TCP/IP. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() ha fallado: razón: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Intentando conectar a '$address' en el puerto '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() ha fallado.\nRazón: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connexion: Cerrada\r\n\r\n";
$out = '';
echo "Enviando una solicitud HTTP HEAD...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Leyendo la respuesta:\n\n";
$buf = 'Este es mi búfer.';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo "$bytes bytes leídos desde socket_recv(). Cerrando el socket...";
} else {
echo "socket_recv() ha fallado; razón: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);
echo $buf . "\n";
echo "OK.\n\n";
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
<h2>Conexión TCP/IP</h2>
OK.
Intentando conectar a '208.77.188.166' en el puerto '80'...OK.
Enviando una solicitud HTTP HEAD...OK.
Leyendo la respuesta:
123 bytes leídos desde socket_recv(). Cerrando el socket...HTTP/1.1 200 OK
Date: Mon, 14 Sep 2009 08:56:36 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: "b80f4-1b6-80bfd280"
Accept-Ranges: bytes
Content-Length: 438
Connexion : Cerrada
Content-Type: text/html; charset=UTF-8
OK.
]]>
</screen>
</example>
</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
-->