Perl en Español

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

Ayuda con Socket

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Experto
Mensaje Vie Jul 11, 2008 9:20 am
escanda
Perlero Nuevo
Perlero Nuevo
Registrado: 11 Jul 2008
Mensajes: 28
Ayuda con Socket Responder citando

Hola a todos ¿Qué tal? Yo soy novato en Perl y por eso les pido que por favor me ayuden, necesito que dos pc se puedan hablar entre sí. Para eso escribí el siguiente código.

Servidor:
Perl:
#!/usr/bin/perl -w
use strict;
use Socket;

# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
   or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
   or die "Can't set socket option to SO_REUSEADDR $!\n";

my $paddr = sockaddr_in($port, INADDR_ANY);

# bind to a port, then listen
bind( SOCKET, pack( 'Sn4x8', AF_INET, $port, "\0\0\0\0" ))
       or die "Can't bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
        # send them a message, close connection
        print NEW_SOCKET "PROBANDO";
        close NEW_SOCKET;
}


Cliente:
Perl:
#!/usr/bin/perl -w
# client.pl
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || `hostname`;
my $port = shift || 7890;
my $server = "192.168.10.58";

# create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
   or die "Can't create a socket $!\n";
connect( SOCKET, pack('Sn4x8', AF_INET, $port, $server))
       or die "Can't connect to port $port! \n";

my $line;
while ($line = <SOCKET>) {
        print "$line\n";
}
close SOCKET or die "close: $!";


A la hora de probar esto no pasa nada y me aparecen errores, así que les pido por favor ayuda.

Gracias.
Mensaje Vie Jul 11, 2008 10:02 am
explorer
Moderador
Moderador
Registrado: 24 Jul 2005
Mensajes: 4105
Ubicación: Valladolid, España
Responder citando

Bienvenido a los foros de Perl en Español, escanda.

En estos foros hay algunos ejemplos de clientes y servidores. ¿Cómo son esos errores?
Mensaje Vie Jul 11, 2008 10:08 am
escanda
Perlero Nuevo
Perlero Nuevo
Registrado: 11 Jul 2008
Mensajes: 28
Responder citando

En el caso del servidor me dice
Código:
Argument "\0\0\0\0" isnt numeric in pack at C:Documents........

y en el caso del cliente me dice:
Código:
Argument "192.168.10.58" isnt numeric in pack at C:Documents......


Desde ya gracias. saludos
Mensaje Vie Jul 11, 2008 1:33 pm
creating021
Vive para Perl en Español
Vive para Perl en Español
Registrado: 23 Feb 2006
Mensajes: 486
Ubicación: Frente al monitor
Responder citando

Perl:
#!/usr/bin/perl -w
# client.pl
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift or `hostname`; # esto no sirve de nada!
my $port = shift or 7890;
my $server = "192.168.10.58";

# create the socket, connect to the port
socket( SOCKET, AF_INET, SOCK_STREAM, getprotobyname('tcp') )
   or die "Can't create a socket $!\n";
my $sin = sockaddr_in( $port, inet_aton( $server ) );

if ( connect ( SOCKET, $sin ) ) {
    while ( my $line = <SOCKET> ) {
        print "$line\n";
    }
}
else { die "Can't connect to $server : $!\n"; }
shutdown( SOCKET, 2 ) or die "close: $!";


Debería de funcionar.

Editado

En cuanto al servidor:
Perl:
#!/usr/bin/perl -w
use strict;
use Socket;

# use port 7890 as default
my $port = shift or 7890;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
   or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
   or die "Can't set socket option to SO_REUSEADDR $!\n";

my $paddr = sockaddr_in($port, INADDR_ANY);

# bind to a port, then listen
my $in = sockaddr_in( $port, INADDR_ANY);
bind( SOCKET,$in )
       or die "Can't bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
        # send them a message, close connection
        print NEW_SOCKET "PROBANDO";
        close NEW_SOCKET;
}


Te recomiendo Perl Coockbook e IO::Socket.
Publicar nuevo tema   Responder al tema    Foros de discusión -> Experto Todas las horas son GMT - 6 Horas
Página 1 de 1



Powered by phpBB © 2001, 2005 phpBB Group