Herong's Tutorial Notes on Perl - Part B
Dr. Herong Yang, Version 4.11

Socket Communication

Part:   1  2   3  4 

(Continued from previous part...)

listen() - Sets a socket handle to listen mode with the specified queue size of incoming connection requests. This function is only used by the server application. For example:

   $queueSize = 5;
   listen(SOCK, $queueSize);

accept() - Waits for incoming connection requests on the specified socket handle, accepts the first connection request, creates a new socket handle, one end bound to the same local address as the specified socket handle, the other end bound to the remote address received in the connection request, and returns the remove address. If no error, the new socket handle is ready for data transmission. This function is only used by the server application. For example:

   $address = accept(NEWSOCK,SOCK);

connect() - Sends a communication request to a remote address that represents a port on a remote system. When the remote system accepted the request, the specified socket handle will be bound to the remote address. If no error, the specified socket handle is ready for data transmission. This function is only used by the client application. For example:

   $domain = 2; # Internet domain
   $host = pack('C4', 216,109,118,67); # www.yahoo.com = 216.109.118.67
   $port = 80; 
   $address = pack('S n a4 x8', $domain, $port, $host);
   connect(SOCK, $address);

The following diagram shows the steps involved in establishing an Internet socket communication link using the built-in functions:

       Client System            Server System
       Internet Address #a      Internet Address #b
Step   Available Port #p        Available Port #q
1                               socket(SOCK,2,1,6);
2                               bind(SOCK,#b+#q);
3                               listen(SOCK,5);
4                               accept(NEWSOCK,SOCK);
5      Socket(SOCK,2,1,6);      (waiting)
6      bind(SOCK,#a+#p);        (waiting)
7      connect(SOCK,#b+#q);     (receiving request)
8      (estalishing the link)   (establishing the link)
9      (SOCK is ready)          (NEWSOCK is ready)

ReverseEchoer.pl - A Simple Socket Server Application

The following program called ReverseEchoer.pl is a simple socket server application, which listens for a connection request. Once connected, it reads what lines of text from the client application, reverses the text lines, and echoes back to the remote application:

#- ReverseEchoer.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
   $domain = 2; # Internet domain
   $type = 1; # Sequenced, reliable, two-way connection, byte streams
   $proto = 6; # Transmission Control Protocol (TCP)
   socket(SOCK,$domain,$type,$proto);
   $host = pack('C4', 0,0,0,0); # Local wildcard host id: 0.0.0.0
   $port = 8888;
   $address = pack('S n a4 x8', $domain, $port, $host);
   bind(SOCK, $address);
   $queueSize = 5; # Queue up to 5 connections
   listen(SOCK, $queueSize);
   print STDOUT "Server host: ",join('.',unpack('C4', $host)),"\n";
   print STDOUT "Server port: $port\n";
   $cAddress = accept(NEWSOCK,SOCK);
   ($cDomain, $cPort, $cHost) = unpack('S n a4 x8', $cAddress);
   print STDOUT "Client host: ",join('.',unpack('C4', $cHost)),"\n";
   print STDOUT "Client port: $cPort\n";
   select(NEWSOCK); $| = 1; select(STDOUT);
   print NEWSOCK "Welcome to Reverse Echo Server.\r\n";
   while ($m=<NEWSOCK>) {
      $m =~ s/\n|\r//g;
      last if ($m eq ".");
      $m = reverse($m);
      print NEWSOCK "$m\r\n";
   }
   close(NEWSOCK);
   exit;

Note that:

  • The socket is bound to the local system with the wildcard host address 0.0.0.0, which represents any IP address on the local host system.
  • Setting $| to 1 is to turn on auto flush to the default file handle. In order to turn on auto flush to NEWSOCK, I have to use select() function to switch default file handles.
  • If a text line received only contains a single character '.', the communication link will be terminated by the close() call.

Run ReverseEchoer.pl, you will get the following output on the console window:

Server host: 0.0.0.0
Server port: 8888

This tells us that the program is ready to accept request at address 0.0.0.0 and port 8888.

To test ReverseEchoer.pl, we can use an existing client program called telnet to initiate the communication request and talk to ReverseEchoer.pl. Open another command window and type in the following command:

telnet localhost 8888

Immediately, you will see more output on the console window of ReverseEchoer:

Client host: 127.0.0.1
Client port: 1032

This tells us that the server socket received a connection request, and a communication link has be established with the client application, which is the telnet program running on the same machine. The client host, 127.0.0.1, is the standard IP address for "localhost". The client port, 1032, was picked up by the telnet program.

(Continued on next part...)

Part:   1  2   3  4 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part B - Socket Communication