- A Simple Socket Client Program

This section provides a tutorial example on how to write a simple socket communication client program,, 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,, to communicate with

#- Copyright (c) All Rights Reserved.
   $domain = 2; # Internet domain
   $type = 1; # Sequenced, reliable, two-way connection, byte streams
   $proto = 6; # Transmission Control Protocol (TCP)
   $host = pack('C4', 127,0,0,1); # localhost =
   $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 =
   $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;

Note that:

Now, run, then run 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:
Client port: 1024
Server host:
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

 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 - A Simple Socket Server Program - A Simple Socket Client Program

 gethostbyaddr() - Network Utility Functions - 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 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


 Full Version in PDF/EPUB