#!/opt/sparc/bin/perl5 -Tw

require 5.002;
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;

sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
my $port = shift || 2345;
my $proto = getprotobyname('tcp');
socket(SERVER, PF_INET, SOCK_STREAM, $proto) || die "socket : $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt : $!";
bind(SERVER, sockaddr_in($port, INADDR_ANY)) || die "bind : $!";
listen(SERVER,SOMAXCONN) || die "listen : $!";

logmsg "server started on port $port";

my ($paddr,$data);

$SIG{CHLD} = \&REAPER;

for ( ; $paddr = accept(CLIENT,SERVER); close CLIENT) {
	my($port,$iaddr) = sockaddr_in($paddr);
	my $name = gethostbyaddr($iaddr,AF_INET);

	logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port";
	
	print CLIENT "Hello there, $name, Bit's now ", scalar localtime, "\n";

		print "bla!\n";
	while (undef($data)) {$data = <CLIENT>;}
        while (!($data =~ /(\\\\)*\\B/)) {
                while ($data =~ s/\\\\/\\/) { }
                print $data;
                $data = "uh-huh?\n";
		print CLIENT $data;
		while (undef($data)) { $data = <CLIENT>; }
        }

}

