Re: php-gtk y sockets
| From: | Jaime A. Chavarriaga L. | Date: | Mon, 28 Jan 2002 22:00:52 +0000 |
| Subject: | Re: php-gtk y sockets | ||
| References: | 1 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-18523@lists.php.net to get a copy of this message | ||
J Eduardo
> Bueno esoty comenzandoa trabajar con php-gtk,
> y tengo ciertas dudas
> 1. todas las funciones de php funciones en gtk
Si la pregunta es... "todas las funciones de PHP funcionan en GTK ??"... si,
básicamente si !!... naturalmente las relacionadas con Cookies y con Apache
no hacen nada !!
la documentación de PHP-GTK esta en...
http://gtk.php.net/docs.php
el sitio de PHP-GTK es...
http://gtk.php.net/
> 2. como me conecto por sockets en php, un ejemplo por favor
Bueno... luego de los múltiples cambios, el API de sockets de PHP 4.0.6
cambió en 4.0.8 y en 4.1.1... básicamente cambió la nueva nomenclatura de
las funciones y ahora todas las funciones se llaman "socket_*"
como ejemplo... siempre puedes ver mi anotación al manual en...
http://www.php.net/manual/en/ref.sockets.php
el ejemplo que aparecía en el manual (a principios del año) esta malo y no
funciona !!
aqui estan los ejemplos...
== Ejemplo de servidor
<?php
/* TCP/IP Server Test
sample for PHP >= 4.1.0
*/
echo "TCP/IP Connection - Server Test\n\n";
/* Allow the script to hang around waiting for connections. */
set_time_limit (0);
/* server configuration */
$address = 'localhost';
$service_port = 10000;
echo "Creating socket...\n";
if (($sock = socket_create (AF_INET, SOCK_STREAM, 0)) < 0) {
echo "socket_create() failed: reason: " . socket_strerror
($sock) . "\n";
}
echo "Binding socket to
".$address.":".$service_port."\n";
if (($ret = socket_bind ($sock, $address, $service_port)) < 0) {
echo "socket_bind() failed: reason: " . socket_strerror($ret) . "\n";
}
echo "Listening in the socket....\n";
if (($ret = socket_listen ($sock, 5)) < 0) {
echo "socket_listen() failed: reason: " . socket_strerror($ret) . "\n";
}
do {
echo "Waiting for connection...\n";
if (($msgsock = socket_accept($sock)) < 0) {
echo "socket_accept() failed: reason: " .socket_strerror ($msgsock)
. "\n";
break;
}
do {
$buf = '';
echo "Reading socket...\n";
$buf = socket_read ($msgsock, 2048);
$buf = trim ($buf);
if (strlen($buf) > 0) {
if ($buf == 'quit') {
socket_close ($msgsock);
break 2;
}
$talkback = "PHP: You said '$buf'.\n";
socket_write ($msgsock, $talkback, strlen ($talkback));
echo "$buf\n";
}
} while (true);
socket_close ($msgsock); /* close incoming socket */
} while (true);
socket_close ($sock); /* close server socket */
?>
== Ejemplo de cliente...
<?php
error_reporting (E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname ('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname ('www.php.net');
/* Create a TCP/IP socket. */
echo "Creating socket...\n";
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror($socket) .
"\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect ($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " .
socket_strerror($result) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.0\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write ($socket, $in, strlen ($in));
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read ($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close ($socket);
echo "OK.\n\n";
?>
8-)
jaime