Welcome to WebmasterWorld Guest from 107.23.37.199

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

mini perl web server using IO::Socket

     
1:44 am on Sep 1, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:June 17, 2000
posts:2924
votes: 0


I sort of hacked this together, I wasn't able to find a good example of a socket script that would keep listening to a port while it spawned and killed it's children.

I'm using this code as a part of a simple p2p application I'm working on, but I thought someone else might find it useful for something, or entertaining.


#!/usr/bin/perl -w

use strict;
use IO::Socket;

sub Wait {
wait;#wait needed to keep <defunct> pids from building up
}

$SIG{CHLD} = \&Wait;

my $server = IO::Socket::INET->new(LocalPort => 6800,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10) or die "$@\n";
my $client ;

while ( $client = $server->accept())
{

next if my $pid = fork;
die "fork - $!\n" unless defined $pid;

##Grab the handoff from client if you want
# my $client_info;
#
# while(<$client>)
# {
# last if /^\r\n$/;
# $client_info .= $_;
# }
###Hold client info for something useful
##$client_stuff;

select $client;
$ = 1;
print $client "HTTP/1.0 200 OK\r\n";
print $client "Content-type: text/html\r\n\r\n";

print $client '<H1>Hello World(!), from a perl web server</H1>';

close($client);
exit( fork );
}
continue
{
close($client); #kills hangs
kill CHLD => -$$;
}

2:49 am on Sept 1, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:June 17, 2000
posts:2924
votes: 0


Oh, if you want to test it out you could type in [127.0.0.1:6800...] into your browser.