LJ Archive

Listing 10. getservertype2.pl

#!/usr/bin/perl -w
# getservertype2.pl - a forking version
use strict;
use IO::Socket;
$|=1;
my @hosts = qw(
    www.ssc.com
    www.linuxjournal.com
    www.perl.com
    www.perl.org
    www.nytimes.com
    www.onsight.com
    www.avue.com
);
sub doit {
    my $server = shift;
    print "processing $server...\n";
    my $sock = new IO::Socket::INET(
                    PeerAddr => $server,
                    PeerPort => 80,
                    Proto    => 'tcp');
    $sock or die "no socket for $_: $!";
    print $sock "GET / HTTP/1.0\n\n";
    while (<$sock>) {
   if (/^Server:\s*(.*)/) {
       print "    $server: $1\n";
       last;
   }
    }
}
my $kid;
for (@hosts) {
    # fork, and if this is the parent,
    # go to the next servername
    next if $kid = fork;
    die "fork: $!" unless defined $kid;
    # this is the child, so process
    # servername
    doit $_;
    exit;
}
# reap all the children
1 while wait != -1;
print "Parent exiting...\n";
LJ Archive