#!/usr/bin/perl # # ICS 243d - Internet Technology # Spring Quarter 2001 # Roberto Silveira Silva Filho ID# 85849631 # Simple Web Server # Assignment 3 # # This server only allows the read of files test1.html and test2.html. # The exit command kills the server. # # based on file: tcp_echo_serv1.pl # form the Book: Network Programming with Perl by Lincoln D. Stein # ---------------------------------------------------------- # usage: tcp_echo_serv1.pl [port] 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 .= ""; $returnMessage .= "
Open the file: http://rodan.ics.uci.edu:$port/test1.html\n\n"; print "
Open the file: http://rodan.ics.uci.edu:$port/test2.html\n\n"; print "
Kill the server: http://rodan.ics.uci.edu:$port/kill\n\n"; print "
\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 (