in reply to Re: perl only listen in 127.0.0.1
in thread perl only listen in 127.0.0.1

This is the server code.

To run i use: perl server.pl 777

use strict; use IO::Socket; #tomar el puerto a controlar o por default 777 my $port = $ARGV[0] || 777; #Variables para la configuracion del servidor remoto a quien pasarle #los parametros del GPS my $remotehost = $ARGV[1] || "localhost"; my $remotepag = $ARGV[2] || "/puntogps.php"; my $rcvdata; #ignorar procesos hijos para evitar zombies $SIG{CHLD} = 'IGNORE'; #crear el socket a escuchar my $listen_socket = IO::Socket::INET->new(LocalPort => $port, LocalAddr => 'localhost', Listen => 1000, Proto => 'tcp', Reuse => 1); #asegurarnos que estamos controlando el puerto die "Cant't create a listening socket: $@" unless $listen_socket; warn "Server ready. Waiting for connections ...", $port, "\n"; #esperar conexiones while (my $connection = $listen_socket->accept){ my $child; # crear el fork para salir die "Can't fork: $!" unless defined ($child = fork()); #¡el hijo! if ($child == 0){ #cerrar el puerto para escuchar el proceso hijo $listen_socket->close; #llamar a la funcion que hace el request al servicio web $rcvdata = <$connection>; callws($remotehost, $remotepag, $rcvdata); #si el hijo regresa salte exit 0; } #¡soy el padre! else{ #¿quién se conectó? warn "Connecton received ... ",$connection->peerhost,"\n"; #cerrar la conexión, ya fue mandada a un hijo $connection->close(); } #regresa a escuchar otras conexiones } sub callws{ my $rh = shift; my $rp = shift; my $rd = shift; my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $rh, PeerPort => "http(80)" ); unless ($remote) { die "cannot connect to http daemon on $rh" } $remote->autoflush(1); print $remote "GET $rp HTTP/1.0\n\n"; while ( <$remote> ) { print } close $remote; }

Replies are listed 'Best First'.
Re^3: perl only listen in 127.0.0.1
by NetWallah (Canon) on May 23, 2009 at 15:40 UTC
    Your specification:
    my $listen_socket = IO::Socket::INET->new(LocalPort => $port, LocalAddr => 'localhost', # <<<<<<<<< H E R E <<<<<<<< Listen => 1000, Proto => 'tcp',
    Tells the server to bind to the localhost.

    Simply remove or comment out the line, and it will bind to all interfaces.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

      Thank you so much, now my script run very well :)