School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,461 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,668 people online right now. Registration is fast and FREE... Join Now!




Perl - Sending Messages between computers

 

Perl - Sending Messages between computers

Ed_Bighead

22 Jun, 2009 - 12:22 PM
Post #1

D.I.C Head
Group Icon

Joined: 26 Apr, 2009
Posts: 166



Thanked: 14 times
Dream Kudos: 50
My Contributions
So I've just started working with networking in perl and I'm not quite sure how it works. Here's what I have so far:

Computer One
use strict;
use warnings;
use IO::Socket;

# Create Socket for recieving messages
my $sockIn = new IO::Socket::INET (
#LocalAddr => 'xx.xxx.xxx.xxx',
LocalPort => '7070',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
)
or die "Could not create socket: $!\n";

# Listen for messages
my $new_sock = $sockIn->accept();
while(<$new_sock>) {
print $_;
}
close($sockIn);
print "SockIn Closed.\n";

# Create Socket for sending messages
my $sockOut = new IO::Socket::INET (
PeerAddr => '10.0.2.15',
PeerPort => '7070',
Proto => 'tcp',
)
or die "Could not create socket: $!\n";

#Send message
print $sockOut "I'm well.\n";
close($sockOut);
print "Done\n";


Computer Two
use strict;
use warnings;
use IO::Socket;

# Create Socket for sending messages
my $sockOut = new IO::Socket::INET (
PeerAddr => '192.168.1.15',
PeerPort => '7070',
Proto => 'tcp',
)
or die "Could not create socket: $!\n";

# Send messages
print $sockOut "Hello there!\n";
sleep 1;
print $sockOut "How are you?\n";
close($sockOut);

print "SockOut Closed.\n";

# Create Socket for receiving messages
my $sockIn = new IO::Socket::INET (
#LocalAddr => 'xx.xxx.xxx.xxx',
LocalPort => '7070',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
)
or die "Could not create socket: $!\n";

print"SockIn Created.\n";

# Listen for messages
my $new_sock = $sockIn->accept();
while(<$new_sock>) {
print $_;
}
close($sockIn);

print "Done\n";


So computer one runs the first code - It creates a socket that listens and sends a message back after it receives a message. Computer two does the opposite (runs the second code), it sends a message and then listens for a response.

Here's the output:
comp 1
Hello there!
How are you?
SockIn Closed.
Could not create socket: Unknown Error

comp 2
SockOut Closed.
SockIn Created.


Computer two is a virtual machine running CentOS; ifconfig shows that the ip is 10.0.2.15, but it doesn't seem to want to connect.
Any help is appreciated.

This post has been edited by Ed_Bighead: 23 Jun, 2009 - 04:32 AM

User is offlineProfile CardPM
+Quote Post


dsherohman

RE: Perl - Sending Messages Between Computers

23 Jun, 2009 - 04:33 AM
Post #2

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
I'm not in a position to test run your code at the moment, but it looks to me like comp1 may be attempting to connect to comp2 before comp2 has created its receiving socket. Have you tried having comp1 sleep for a couple seconds after it closes SockIn and before it opens SockOut?

Also, have you verified that comp1 knows how to route to 10.x.x.x? If comp2 is multi-homed or behind a NAT router, comp1 may be seeing it at a different address. (If NAT is the culprit, you'll need to set up port forwarding on the router to get this to work...) Try having comp1 print out the remote IP address after the $sockIn->accept so that you'll know what IP address it sees at the other end of the connection.
User is offlineProfile CardPM
+Quote Post

Ed_Bighead

RE: Perl - Sending Messages Between Computers

25 Jun, 2009 - 05:57 AM
Post #3

D.I.C Head
Group Icon

Joined: 26 Apr, 2009
Posts: 166



Thanked: 14 times
Dream Kudos: 50
My Contributions
That's certainly possible, I'll try sleeping like you suggested. Also, what is the command / syntax to get the IP address that comp1 is seeing?

EDIT:
I'm pretty sure the problem is that the ip address is invalid - when comp1 tries to create the socket to send a message back, it fails and gives me the error above - Could not create socket: Unknown Error.

This post has been edited by Ed_Bighead: 25 Jun, 2009 - 06:20 AM
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Perl - Sending Messages Between Computers

26 Jun, 2009 - 04:31 AM
Post #4

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(Ed_Bighead @ 25 Jun, 2009 - 01:57 PM) *

Also, what is the command / syntax to get the IP address that comp1 is seeing?


According to The IO::Socket documentation:
QUOTE
METHODS
...
connected

If the socket is in a connected state the peer address is returned. If the socket is not in a connected state then undef will be returned.


So $new_sock->connected should tell you the IP address it's connected to.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:19AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month