#!/usr/bin/perl -w # # Example 9-9. The server. use IO::Socket; # Part of the base Perl distribution, use IO::Select; # these are used for implementing socket # communication my $server = new IO::Socket::INET( LocalPort => 9999, Listen => 10); my $select = IO::Select->new($server); my @messages; # The outgoing message buffer $/="\0"; # Set the line input separator print STDERR "Server started...\n"; # Loop in 'daemon mode' until killed while (1) { @messages = (); # Loop through the open handles that are ready with data foreach my $client ($select->can_read(1)) { if ($client==$server) { # In this case, we have a new connection request $client = $server->accept(); $select->add($client); } else { # Read the data from the client and push it on # the buffer, if the client is still there my $data = <$client>; if (defined($data)) { push @messages, $data; } else { $select->remove($client); $client->close; } } } # Loop through the handles waiting for input # and send them the buffered messages foreach my $client ($select->can_write(1)) { foreach my $m (@messages) { print $client "$m"; } } }