#!/usr/bin/perl # # ICS 243d - Internet Technology # Spring Quarter 2001 # Roberto Silveira Silva Filho ID# 85849631 # UnixADM daemon # Finl project # # This server provides information about the current host. # # ---------------------------------------------------------- use strict; use Socket; use IO::Handle; use IO::File; $| = 1; # No buffering use constant MY_WEB_PORT => 16004; my $port = shift || MY_WEB_PORT; my $protocol = getprotobyname('tcp'); # Returns a message in the html page # @param message the messa that is going to be printed in the page. sub returnMessage { my $message = shift; # my $returnMessage = "Content-type: text/html\n\n"; my $returnMessage .= "\n"; $returnMessage .= "
\n\n";
socket(SOCK, AF_INET, SOCK_STREAM, $protocol) or die "socket() failed: $!";
setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1) or die "Can't set SO_REUSADDR: $!" ;
my $my_addr = sockaddr_in($port,INADDR_ANY);
bind(SOCK,$my_addr) or die "bind() failed: $!";
listen(SOCK,SOMAXCONN) or die "listen() failed: $!";
warn "waiting for incoming connections on port $port...\n";
# Stays in an infinite loop until it receives an exit command...
while (1) {
next unless my $remote_addr = accept(SESSION,SOCK);
my ($remotePort,$hisaddr) = sockaddr_in($remote_addr);
warn "Connection from [",inet_ntoa($hisaddr),",$remotePort]\n";
SESSION->autoflush(1);
# Generates a page whenever the server is accesse by a web server.
# It dies after the generation.
while ( UnixAdm server was KILLED
\n");
print "Exiting...";
exit(0);
} else {
print SESSION returnMessage ("Request: $argument not valid!");
}
# print SESSION "Connection: close\n";
# warn $command[0];
close SESSION;
}
warn "Closing session...\n";
close SESSION;
warn "Connection from [",inet_ntoa($hisaddr),",$port] finished\n";
}
warn "Socked connection closed \n";
close SOCK;