#!/usr/bin/perl -w use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr $PORT = 23; # pick something not in use $crlf = "\015\012"; $server = IO::Socket::INET->new(Proto => 'tcp', LocalPort => $PORT, Listen => 10, #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.$crlf"; $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost; print $client "Command? $crlf"; while ( <$client>) { print "got $_";; next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/date|time/i) { printf $client "%s$crlf", scalar localtime; } elsif (/who/i ) { print $client $client->peerhost,$crlf; } elsif (/cookie/i ) { print $client "cookie$crlf"; } elsif (/motd/i ) {print "showing motd\n"; print $client "motd$crlf"; } else { print $client "Commands: quit date who cookie motd$crlf"; } } continue { print $client "Command? $crlf"; } close $client; }