#!/usr/bin/perl
use IO::Select;
use IO::Socket;
use Data::Dumper;
use strict;
my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 31337) or die "Cannot start server: $!";
my $sel = new IO::Select( $lsn );
while (my @ready = $sel->can_read) {
foreach my $fh (@ready) {
if ($fh == $lsn) {
# Create a new socket
my $new = $lsn->accept;
$sel->add($new);
print "Server: New connection established from " . Dumper($fh) . " !\n";
} else {
if ($fh) {
my $line=<$fh>;
if ($line) {
$line =~ s/\r|\n$//g;
print "[$line]\n";
if ($line =~ /^quit$/i) {
print $fh "-- Close connection!\n";
$sel->remove($fh);
$fh->close;
}
}
} else {
# Maybe we have finished with the socket
$sel->remove($fh);
$fh->close;
}
}
}
}