SocketClient.pl - A Simple Socket Client Program

This section provides a tutorial example on how to write a simple socket communication client program, SocketClient.pl, which creates a socket and makes a connection request to a remote socket communication server.

Instead of using the telnet program, we can use the following program, SocketClient.pl, to communicate with ReverseEchoer.pl:

#- SocketClient.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   $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', 127,0,0,1); # localhost = 127.0.0.1
   $port = 1024; 
   $address = pack('S n a4 x8', $domain, $port, $host);
   bind(SOCK, $address);
   print STDOUT "Client host: ",join('.',unpack('C4', $host)),"\n";
   print STDOUT "Client port: $port\n";
   $sHost = pack('C4', 127,0,0,1); # localhost = 127.0.0.1
   $sPort = 8888; 
   $sAddress = pack('S n a4 x8', $domain, $sPort, $sHost);
   connect(SOCK, $sAddress);
   print STDOUT "Server host: ",join('.',unpack('C4', $sHost)),"\n";
   print STDOUT "Server port: $sPort\n";
   select(SOCK); $| = 1; select(STDOUT);
   while ($m=<SOCK>) {
      print STDOUT $m;
      $m = <STDIN>;
      print SOCK $m;
   }
   close(SOCK);
   exit;

Note that:

Now, run ReverseEchoer.pl, then run SocketClient.pl in a separate command window. If you type in the same text as in the previous test, you will get the following output:

Client host: 127.0.0.1
Client port: 1024
Server host: 127.0.0.1
Server port: 8888
Welcome to Reverse Echo Server.
Fish, I love you and respect you very much.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
But I will kill you dead before this day ends.
.sdne yad siht erofeb daed uoy llik lliw I tuB
.

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

 Converting Perl Script to Executable Binary

 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

 References

 PDF Printing Version