use strict ; use warnings ; use IO::Socket::INET ; use Socket ; select STDERR ; $|++ ; select STDOUT ; $|++ ; my $server = IO::Socket::INET->new(LocalPort => 3575, Reuse => 1, LocalHost => 'localhost', Listen => 5) or die "Couldn't create Server $! ($@)\n" ; my @ignore ; foreach my $action (2, # receive request, send 2 replies & close 1, # receive request, send 1 reply & close 0, # receive request, send 0 reply & close -1, # receive request, ignore & DON'T close -2, # ignore completely & DON'T close ) { my $client = $server->accept() ; if (!defined($client)) { print STDERR "Accept failed: $! ($@)" ; next ; } ; print "Connection accepted (action = $action)\n" ; my $request = 'connection' ; if ($action > -2) { defined $client->recv($request, 1024) or die "recv request failed $!" ; print STDERR "Request received: '$request'\n" ; } ; if ($action < 0) { push @ignore, $client ; # Not to be closed print STDERR "Ignoring $request, but leaving connection open\n" ; next ; } ; for my $r (1..2) { if ($r <= $action) { my $reply = "$request -- reply $r" ; defined $client->send($reply) or die "send reply-$r failed $!" ; print STDERR "Sent '$reply'\n" ; sleep(2) ; } else { print STDERR "Not sending reply $r\n" ; } }; close $client ; print STDERR "Connection closed\n" ; } ; print STDERR "Ignoring everything" ; while(1) { sleep(10) ; print STDERR "." ; } ;