The Start of Something Beautiful
#! /usr/bin/perl
use strict;
use warnings;
use Event;
use IO::Socket;
my $socket = new IO::Socket::INET(
LocalPort => 2000,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 20
);
Event->timer(
after => 30,
cb => sub { print("Finished\n"); exit 0; },
);
Event->io(
fd => $socket,
cb => sub { handle_connection(@_) },
);
Event::loop();
sub handle_connection {
my ($event) = @_;
my $socket = $event->w()->fd();
my $client = $socket->accept();
print "Someone connected $client\n";
}
./example3.pl &
for i in 0 1 2 3 4 5 6 7 8 9; do echo "Hello" | nc localhost 2000; done
Someone connected IO::Socket::INET=GLOB(0x82dec4c)
Someone connected IO::Socket::INET=GLOB(0x82dec58)
Someone connected IO::Socket::INET=GLOB(0x82dec64)
Someone connected IO::Socket::INET=GLOB(0x82deca0)
Someone connected IO::Socket::INET=GLOB(0x82dec94)
Someone connected IO::Socket::INET=GLOB(0x82dec40)
Someone connected IO::Socket::INET=GLOB(0x82decb8)
Someone connected IO::Socket::INET=GLOB(0x82decac)
Someone connected IO::Socket::INET=GLOB(0x820e9f8)
Someone connected IO::Socket::INET=GLOB(0x82dec58)
Finished