That code is from `perldoc perlipc` and does work.
Here's a (slightly modified) version from "Network Programming with Perl by Lincon Stein".
server.pl #!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use constant ECHO_PORT => 9000;
my ($bytes_out, $bytes_in, $quit) = (0,0,0);
$SIG{INT} = sub { $quit++ };
my $port = shift || ECHO_PORT;
my $sock = IO::Socket::INET->new(Listen => 20,
LocalPort => $port,
Timeout => 60,
Reuse => 1,
) or die "Can't create listening socket: $!\n";
warn "waiting for connections on port $port...\n";
while ( !$quit ) {
next unless my $session = $sock->accept;
my $peer = gethostbyaddr($session->peeraddr, AF_INET) || $session->peerhost;
my $port = $session->peerport;
warn "Connection from [$peer, $port]\n";
while ( <$session> ) {
$bytes_in += length($_);
$bytes_out += length($_);
print "client $peer sent '$_'";
print $session $_;
$quit++ if $bytes_in eq 'q';
}
warn "Connection from [$peer, $port] finished\n";
close $session;
}
warn "bytes_received = $bytes_in, bytes_sent = $bytes_out\n";
close $sock or warn $@;
client.pl #!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
my ($bytes_out, $bytes_in) = (0,0);
my $host = shift || 'localhost';
my $port = shift || 9000;
my $socket = IO::Socket::INET->new( Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port,
) or die "can't connect to port $port on $host: $!";
$socket->autoflush(1);
while ( my $msg_out = STDIN->getline ) {
last if $msg_out =~ /^q/i;
print $socket $msg_out;
my $msg_in = <$socket>;
print $msg_in;
$bytes_out += length($msg_out);
$bytes_in += length($msg_in);
}
$socket->close or warn $@;
print "bytes_sent = $bytes_out, bytes_received = $bytes_in\n";
If that doesn't work for you, then you'll need to provide more details, such as:
1) A copy/paste of both the server and client side output.
2) What version and distro of perl are you using?
3) Are you able to run other scripts?
3) Is your firewall enabled?
4) What AV software are you using?
5) Are both scripts on the same LAN?
6) Are both scripts on the same host?