-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathexamples.xml
More file actions
166 lines (145 loc) · 4.7 KB
/
examples.xml
File metadata and controls
166 lines (145 loc) · 4.7 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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: af4410a7e15898c3dbe83d6ea38246745ed9c6fb Maintainer: seros Status: ready -->
<chapter xml:id="sockets.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<para>
<example>
<title>Ejemplo de socket: Servidor TCP/IP sencillo</title>
<para>
Este ejemplo muestra una respuesta de servidor simple. Cambie las
variables <varname>address</varname> y <varname>port</varname>
para ajustar su configuración y ejecútelo. Debe después conectar el
servidor con un comando similar a: <command>telnet 192.168.1.53
10000</command> (donde la dirección y el puerto deben coincidir con su
cofiguración). Cualquier cosa que escriba será impresa en el lado del
servidor, y vuelta a repetir (echo) para usted. Para desconectar, introduzca 'quit'.
</para>
<programlisting role="php">
<![CDATA[
#!/usr/local/bin/php -q
<?php
error_reporting(E_ALL);
/* Permitir al script esperar para conexiones. */
set_time_limit(0);
/* Activar el volcado de salida implícito, así veremos lo que estamos obteniendo
* mientras llega. */
ob_implicit_flush();
$address = '192.168.1.53';
$port = 10000;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}
do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Enviar instrucciones. */
$msg = "\nBienvenido al Servidor De Prueba de PHP. \n" .
"Para salir, escriba 'quit'. Para cerrar el servidor escriba 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));
do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
break;
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
$talkback = "PHP: Usted dijo '$buf'.\n";
socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
socket_close($msgsock);
} while (true);
socket_close($sock);
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Ejemplo de socket: Cliente TCP/IP sencillo</title>
<para>
Este ejemplo muestra un simple, único cliente HTTP. Simplemente
se conecta a una página, envía una petición HEAD, repite la réplica,
y sale.
</para>
<programlisting role="php">
<![CDATA[
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Obtener el puerto para el servicio WWW. */
$service_port = getservbyname('www', 'tcp');
/* Obtener la dirección IP para el host objetivo. */
$address = gethostbyname('www.example.com');
/* Crear un socket TCP/IP. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() falló: 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() falló.\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 .= "Connection: Close\r\n\r\n";
$out = '';
echo "Enviando petición HTTP HEAD ...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Leyendo respuesta:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "Cerrando socket...";
socket_close($socket);
echo "OK.\n\n";
?>
]]>
</programlisting>
</example>
</para>
</chapter>
<!-- 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
-->