Bottom layers: physical network, IP, TCP
Top layers: FTP, telnet, http
Type of connection: connection-oriented vs. packet-oriented
#!/usr/local/bin/perl
use IO::Socket;
$current_host = "www.slis.indiana.edu";
$current_doc = "/";
$remote =IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $current_host,
PeerPort => "http(80)",
);
if (!$remote) {die "cannot connect to http daemon on $current_host"}
$remote->autoflush(1);
print $remote "GET $current_doc HTTP/1.0\n\n";
$line = <$remote> ;
while ($line) {
print "$line";
$line = <$remote> ;
}
close $remote;
Exercises
Try the script using pages you know. Notice the text that is printed before the "<html>" tag. What happens if the filename is incomplete (for example compare "php.indiana.edu/~upriss" and "php.indiana.edu/~upriss/")? What happens if there exists no file at the requested URL.
Print the output to a file and then view the file through your browser.
Using a regular expression to omit the header information and print only the lines that follow the "<html>" tag.
Turn the script into a subroutine. In a main routine save three hostnames and document names each into an array. Let the main routine call the subroutine three times using each hostname/document name pair once. Don't use global variables but instead pass $current_host and $current_doc as arguments to the subroutine.
Exercise
Incorporate this into the "connecting to http ports" script from above. Let the user input a topic. Display the result without the advertisements.
Note: it may be illegal to incorporate other CGI scripts into your script without asking the owner of the original script for permission. At a minimum you would have to inform users about the underlying script.
Exercise
Review what you have learned about processing forms with CGI so far.
#!/usr/local/bin/perl -w
use IO::Socket;
$current_host ="ftp.uwsg.indiana.edu";
$email_address ="user\@indiana.edu";
$remote =IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $current_host,
PeerPort => "ftp(21)",
);
if (!$remote) { die "cannot connect to http daemon on $current_host"}
$remote->autoflush(1);
print $remote "USER anonymous\n";
print $remote "PASS $email_address\n";
print $remote "quit\n";
while (<$remote>)
{print;}
close $remote;