#!/usr/bin/perl -w use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr $PORT = 9000; # pick something not in use $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n"; while ($client = $server->accept()) { $client->autoflush(1); print $client "Welcome to $0; type help for command list.\n"; $hostinfo = gethostbyaddr($client->peeraddr); my $response = $hostinfo ? $hostinfo->name : $client->peerhost; printf "[Connect from %s]\n", $response; print $client "Command?\n"; while ( <$client>) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/date|time/i) { print $client "SELECTED: date/time\n"; } elsif (/who/i ) { print $client "SELECTED: who\n"; } elsif (/cookie/i ) { print $client "SELECTED: cookie\n"; } elsif (/motd/i ) { print $client "SELECTED: motd\n"; } else { print $client "Commands: quit date who cookie motd\n"; } } continue { print $client "Command?\n"; } close $client; }