#!/usr/bin/perl -w use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr use threads; $PORT = 9000; # pick something not in use $count = 0; my @server; serv: $count++; $server[$count] = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server[$count]; print "[Server $0 accepting clients]\n"; if ($client = $server[$count]->accept()) { $thr1 = threads->new(\&handle); } goto serv; #if ($client = $server[count->accept()) { $thr2 = threads->new(\&handle); } $return_val = $thr1->join; print "$return_val\n"; while (1) { } # while ($client = $server->accept()) { # $client->autoflush(1); # print $client "Welcome to $0; type help for command list.\r\n"; # $hostinfo = gethostbyaddr($client->peeraddr); # printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost; # print $client "Command1 \r\n"; # while ( <$client>) { # next unless /\S/; # blank line # if (/quit|exit/i) { last; } # elsif (/date|time/i) { printf $client "%s\r\n", scalar localtime; } # elsif (/who/i ) { print $client `who 2>&1`; } # elsif (/cookie/i ) { print $client `/usr/games/fortune 2>&1`; } # elsif (/motd/i ) { print $client `cat /etc/motd 2>&1`; } # else { # print $client "Commands: quit date who cookie motd\r\n"; # } # } continue { # print $client "Command2 \r\n"; # } # close $client; # } sub handle { $client->autoflush(1); print $client "Welcome to $0; type help for command list.\r\n"; $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost; print $client "Command1 \r\n"; while ( <$client>) { next unless /\S/; # blank line if (/quit|exit/i) { last; } elsif (/date|time/i) { printf $client "%s\r\n", scalar localtime; } elsif (/who/i ) { print $client `who 2>&1`; } elsif (/cookie/i ) { print $client `/usr/games/fortune 2>&1`; } elsif (/motd/i ) { print $client `cat /etc/motd 2>&1`; } else { print $client "Commands: quit date who cookie motd\r\n"; } } continue { print $client "Command2 \r\n"; } close $client; return 1; }