Perl en Español

  1. Home
  2. Tutoriales
  3. Foro
  4. Artículos
  5. Donativos
  6. Publicidad
 

Socket Server

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Intermedio
Mensaje Mie May 24, 2006 3:57 pm
adivisi
Perlero Nuevo
Perlero Nuevo
Registrado: 24 May 2006
Mensajes: 4
Socket Server Responder citando

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
Mensaje Jue May 25, 2006 4:43 am
explorer
Moderador
Moderador
Registrado: 24 Jul 2005
Mensajes: 4084
Ubicación: Valladolid, España
Responder citando

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;
        }
    }
Mensaje Jue May 25, 2006 1:04 pm
adivisi
Perlero Nuevo
Perlero Nuevo
Registrado: 24 May 2006
Mensajes: 4
Responder citando

Muchas gracias por la ayuda. Modificaré el script y veré cómo me va Smile

Saludos
Publicar nuevo tema   Responder al tema    Foros de discusión -> Intermedio Todas las horas son GMT - 6 Horas
Página 1 de 1



Powered by phpBB © 2001, 2005 phpBB Group