Perl and Networking

Protocols

Bottom layers: physical network, IP, TCP
Top layers: FTP, telnet, http

Type of connection: connection-oriented vs. packet-oriented

Connecting to the http port

In this script "$remote" is a variable for a two directional socket handle (similar to a filehandle, which is usually one directional).

#!/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.

Connecting to other CGI sites

To connect to another CGI script on the web you can submit the information that the script requires via the URL and then display the result through your CGI script. For example, type
http://search.yahoo.com/bin/search?p=WORD
into the URL field of your browser and replace WORD by a topic you want to retrieve.

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.

Optional: Connecting to the ftp port

You need to fill in your email address before you start the script. This script is only an example of how to use the IO::Socket. For serious implementation the NET::FTP module should be used. Another possibility would be to use the http protocol.

#!/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;