Mie May 24, 2006 3:57 pm
|
 |
adivisi
Perlero Nuevo

|
Registrado: 24 May 2006
Mensajes: 4
|
|
| Socket Server |
|
|
Estimados,
Debo realizar un servidor de escucha. Buscando información al respecto, encontré el módulo IO::Socket::INET. Además encontré algunos ejemplos de server. Expongo uno de ellos:
| Código: |
#!/usr/bin/perl -w
use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr
$PORT = 9000;
$MAX_CLIENTS = 10;
$server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => $MAX_CLIENTS, Reuse => 1);
die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";
while ($client = $server->accept()) {
$client->autoflush(1);
print $client "Welcome to \n";
while ( defined( $line = <$client>) ) {
print $client $line . "\n";
}
close $client;
} |
y funciona Ok, pero cuando recibe la segunda conexión, la deja en cola, y no responde hasta que termine con la primera.
¿ A que se debe esto ?
Saludos |
|
|
|

Jue May 25, 2006 4:43 am
|
 |
explorer
Moderador

|
Registrado: 24 Jul 2005
Mensajes: 4084
Ubicación: Valladolid, España
|
|
|
|
|
Eso significa que falta hacer un fork después del accept, para que quede escuchando la siguiente petición mientras procesa la primera. Eso a nivel de proceso. O crear un nuevo thread. O usar select para distinguir las llamadas entrantes.
Tienes aqui un ejemplo hecho con fork:
| Código: |
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my ($host, $port, $kidpid, $handle, $line);
unless (@ARGV == 2) { die "usage: $0 host port" }
($host, $port) = @ARGV;
# create a tcp connection to the specified host and port
$handle = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to port $port on $host: $!";
$handle->autoflush(1); # so output gets there right away
print STDERR "[Connected to $host:$port]\n";
# split the program into two processes, identical twins
die "can't fork: $!" unless defined($kidpid = fork());
# the if{} block runs only in the parent process
if ($kidpid) {
# copy the socket to standard output
while (defined ($line = <$handle>)) {
print STDOUT $line;
}
kill("TERM", $kidpid); # send SIGTERM to child
}
# the else{} block runs only in the child process
else {
# copy standard input to the socket
while (defined ($line = <STDIN>)) {
print $handle $line;
}
} |
|
|

Jue May 25, 2006 1:04 pm
|
 |
adivisi
Perlero Nuevo

|
Registrado: 24 May 2006
Mensajes: 4
|
|
|
|
|
Muchas gracias por la ayuda. Modificaré el script y veré cómo me va
Saludos |
|
Powered by phpBB © 2001, 2005 phpBB Group
|