Sockets in Perl

© Copyright University of New Haven 1999

The examples below use Perl which has a simple Sockets API. Perl is a typeless interpreted language useful for creating complex command scripts. It uses a syntax similar to C language. Perl is already installed on System2, and you can download a freeware copy for your (e.g. MS Windows) machine from www.cpan.org.

Perl Socket Objects

To create a client socket (active open), placing a reference to it in variable $server:
     use IO::Socket;

     $server= IO::Socket::INET->new(
               Proto    => "tcp",
               PeerAddr => "system2.newhaven.edu:80",
               )
               or die "cannot connect to port 80 at system2";
To create a server socket (passive open):
     use IO::Socket;

     $server= IO::Socket::INET->new( 
               Proto     => "tcp",
               LocalPort => 9123,
               Listen    => 5       # Max queue of waiting clients
               )
               or die "can't serve";
After creating a socket and placing a reference to it in variable $server, the server program can accept a client connection, using the $server->accept() method. This method awaits a client connection (at port 9123, in this example) to the socket referenced by $server, assigns a new vacant port number for the connection, then creates and returns a reference to a new socket for this connection. The original port (9123) remains available for further connections from other clients.

After successfully establishing a connection, a line may be read from the socket object referenced (for example, by $server above, or $client below) using a Perl assignment statement with the socket object reference enclosed in < ... >:

     $line= <$server>;
The contents of a variable may also be sent to standard output using:
     print "Received [", $line, "] from server."
or, sent sent through a socket (e.g. $server) using:
     print $server, "Received [", $line, "] from server."

Example of a Perl Web Client

With the following file in client.pl:

     use IO::Socket;

     $server= IO::Socket::INET->new(
               Proto    => "tcp",
               PeerAddr => "system2.newhaven.edu:80",
               )
               or die "cannot connect to port 80 at system2";

     print  $server  "GET /index.html\n";
     $server->flush();
     while  ($line= <$server>)  { print  $line; }
     close  $server;

The command:

perl  client.pl
prints the same output as does the command:
telnet system2.newhaven.edu 80
GET /index.html
The $server->flush() method is required to force the network layer to send the request to the server, before the client attempts to read the server's response in the while loop.

Example of a Perl Web Server

With the following file in server.pl:

     use IO::Socket;

     $server= IO::Socket::INET->new( 
               Proto     => 'tcp',
               LocalPort => 9123,
               Listen    => 5       # Max queue of waiting clients
               )
               or die "can't serve";

     while  ($client= $server->accept())
     {
          $line= <$client>;
          print  $client "Received [", $line, "] from you.\n";
          close  $client;
     }
     close $server;

the command:

perl  server.pl
starts a server listening at port 9123 which does not return to the command prompt. (It can be terminated by control-Z on MS Windows, and control-C on Unix.) While it is running on System2, for example, a web browser can access it at the Uniform Resource Locator (URL) system2.newhaven.edu:9123/index.html. Since this web server isn't listening at the usual HTTP port 80, the non-standard listening port (9123, in this example) must be specified with the phrase ":9123" in the URL given to the browser.

Useful Perl Information

Home Page