#!/usr/bin/perl use Socket; use IO::File; $docRoot = "/home/suman/"; $proto = getprotobyname('tcp'); socket(F, PF_INET, SOCK_STREAM, $proto); # TCP socket $port=80+int(rand(720)); print "The port number is $port \n"; $sin = sockaddr_in($port, INADDR_ANY); # Use port 80 bind(F, $sin); # Bind F to port 80 listen(F,1); # Listen for 1 connection accept(FH, F); # Wait for connection select(FH); $|=1; # Don't buffer I/O select(STDOUT); # Set default print $line = ; # Read first client line $line1 = ; # Read first client line ($get, $line) = split(/GET /, $line); # Locate GET ($fname, $line) = split(/ /, $line); # File name print "Looking at /$docRoot/$fname \n"; $theFile = new IO::File "$docRoot/$fname", "r"; # Open, assume on same drive as server if (defined $theFile) { print FH "HTTP/1.1 200 ok\r\n"; print FH "Server: myserver\r\n"; print FH "Content-Type: text/html \r\n"; print FH "\r\n"; print FH <$theFile>; # Print file to client undef $theFile; } else { print FH "HTTP/1.1 404 Not found\r\n"; print FH "Server: myserver\r\n"; print FH "\r\n"; print FH "

$fname not found!

"; } close(FH);