Hi, thanks in advance for reading this!


Let me first explain the situation:

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 ...

Question:

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.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);}
receiver.pl
#!/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.


In reply to Sending Socket peer to peer by karstenda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.