ReverseEchoer.pl - A Simple Socket Server Program

This section provides a tutorial example on how to write a simple socket communication server program, ReverseEchoer.pl, which listens and accepts for any client requests.

The following program called ReverseEchoer.pl is a simple socket communication 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) HerongYang.com. All Rights Reserved.
#
   $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:

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.

In the telnet window, type in the following text:

Fish, I love you and respect you very much.
But I will kill you dead before this day ends.
.

The text will be reversed and returned back from the ReverseEchoer.pl:

Welcome to Reverse Echo Server.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
.sdne yad siht erofeb daed uoy llik lliw I tuB
.

Note that the telnet program is not displaying text you typed in. It only displays the text received from ReverseEchoer.pl program.

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

Socket Communication Over the Internet

 What Is Socket Communication?

 connect() - Establishing a Socket Communication

ReverseEchoer.pl - A Simple Socket Server Program

 SocketClient.pl - A Simple Socket Client Program

 gethostbyaddr() - Network Utility Functions

 Socket.pm - The Socket Module

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB