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