#!/usr/bin/perl
use strict;
use warnings;
use IO::File;
use IO::Socket;
use IO::Select;
use Data::Dumper;
my $sock = new IO::Socket::INET (
LocalHost => 'perlplexity.org',
LocalPort => 1200,
Listen => 5,
Proto => 'tcp',
Reuse => 1,
);
die "Socket couldn't be created: $!" unless $sock;
$sock->autoflush;
my $message = "";
my $state = "UNCONNECTED";
my $fh = new IO::File "~/.somefile", "r";
my $select = new IO::Select(); # create handle set for reading
$select->add($sock); # add the main socket to the set
$select->add($fh); # add the main socket to the set
while (1) {
my @ready = $select->can_read(0);
my $conn;
my $buff;
foreach my $rh (@ready) {
print "GOT SOMETHING!\n";
if ($rh == $sock) {
$conn = $rh->accept();
$select->add($conn);
print "CONNECTION ADDED\n";
}
elsif ($rh == $conn) {
$buff = "";
my $rx_txt;
while (length($rx_txt)) {
$sock->recv($rx_txt, 1024);
$buff.=$rx_txt;
}
$select->remove($rh);
if ($buff eq "HELLO\n") {
$state = "CONNECTED";
print "CONNECTED\n";
}
else {
$state = "UNCONNECTED";
print "UNCONNECTED\n";
$select->remove($rh);
close($rh);
}
}
elsif ($rh == $fh) {
if ($state == "CONNECTED") {
while (<$fh>) {$sock->send($_);}
print $fh "";
}
}
}
}
####
use strict;
use warnings;
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => 'perlplexity.org',
PeerPort => 1200,
Proto => 'tcp',
);
die "Socket could not be created. Reason: $!\n" unless $sock;
$sock->send("HELLO\n");
close ($sock);
##
##
lofile, obtained from "perl server.pl > log" and the perl client.pl on the other machine
GOT SOMETHING!
CONNECTION ADDED
GOT SOMETHING!
GOT SOMETHING!
GOT SOMETHING!
GOT SOMETHING!
GOT SOMETHING!
GOT SOMETHING!
GOT SOMETHING!
...
this goes on until I interrupt it with ctrl-c