karstenda has asked for the wisdom of the Perl Monks concerning the following question:
Hi, thanks in advance for reading this!
I'm trying to create a sort of instant messenger with perl using sockets, therefore i created two perl scripts (sender.pl and receiver.pl). They work as following: PC 1 (runs sender.pl) will send a socket (containing the message) to another PC 2 (runs receiver.pl) who will read and print the message from the incoming socket.
Problem:My scripts are working fine as long as both PC1 and PC2 are in the same local network (using local IP). If I want to send my sockets over the internet however my script fails. I think I misconfigured the 'PeerAddr' and the 'LocalAddr' in sender.pl.
The port that I'm using in my script to send the socket over (7070) is an open port on the router of PC2, so this can't be the problem I think ...
Is it possible to send peer to peer messages with sockets? If yes, how do I do it? What's my fault?
I have to admit that this is the first time I program with sockets and I'm not really familiar with this kind of perl programming. I just the main part of this script from a website. The tutorials and searches I read about this subject (believe me I did!) weren't really helpfull, in all example scripts is PC 2 (receiver.pl) is just a webserver.
sender.plreceiver.pl#!/usr/bin/perl -w use IO::Socket; $message="Hello server!" #Message you send to server my $sock = new IO::Socket::INET ( PeerAddr => "$ip", #The IP of the network of PC2 LocalAddr => "$localip", #The IP of PC2 in the local network PeerPort => '7070', Proto => 'tcp',); die "Could not create socket: $!\n" unless $sock; print $sock "$message\n"; close($sock);}
#!/usr/bin/perl -w use IO::Socket; my $sock = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 7070, Listen => 1, Reuse => 1, Type => SOCK_STREAM ) or die "Can't create listen socket: $!"; my $new_sock = $sock->accept(); while(<$new_sock>) {$message = $_} close($sock); print "$message";
This is my first post on this forum and I would be really thankfull if you could help me with this problem.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sending Socket peer to peer
by zwon (Abbot) on Dec 07, 2008 at 11:42 UTC | |
by eye (Chaplain) on Dec 07, 2008 at 17:23 UTC | |
by karstenda (Acolyte) on Dec 07, 2008 at 22:58 UTC | |
|
Re: Sending Socket peer to peer
by zentara (Cardinal) on Dec 07, 2008 at 14:33 UTC | |
|
Re: Sending Socket peer to peer
by gone2015 (Deacon) on Dec 07, 2008 at 12:23 UTC | |
|
Re: Sending Socket peer to peer
by Anonymous Monk on Dec 07, 2008 at 10:16 UTC | |
|
Re: Sending Socket peer to peer
by karstenda (Acolyte) on Dec 07, 2008 at 23:01 UTC |