Socket.pm - The Socket Module

This section provides a tutorial example on how to use the Socket.pm module to creating sockets and establish network communication connections.

In the previous examples, there are a lots hard coded values, like socket domain and socket type. The Socket Module is designed to help you to hide those values. It also offers a number of methods to pack and unpack IP addresses and socket addresses.

PF_INET - Returns the socket domain number of Internet domain.

SOCK_STREAM - Returns the socket type number for sequenced, reliable, two-way connection, byte streams

INADDR_ANY - Returns the packed wildcard IP address, similar to pack('C4', 0,0,0,0).

inet_aton("216.109.118.67") - Returns the packed form of the specified IP address, similar to pack('C4', 216,109,118,67). inet_aton("www.yahoo.com") works too.

inet_aton($pHost) - Returns the unpacked form of the specified packed IP address.

pack_sockaddr_in($port, $pHost) - Returns the packed socket address of the specified port number and packed IP address. Internet domain number will be automatically added. This function is similar to pack('S n a4 x8', $domain, $port, $pHost);

unpack_sockaddr_in($address) - Returns port name and packed IP address in an array of the specified packed socket address.

sockaddr_in($port, $pHost) - Same as pack_sockaddr_in($port, $pHost) in scalar context.

sockaddr_in($address) - Same as unpack_sockaddr_in($address) in list context.

I revised the Reverse Echoer server application with the help of Socket Module:

#- ReverseEchoer2.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#
   use Socket;
   $proto = getprotobyname('tcp');
   socket(SOCK,PF_INET,SOCK_STREAM,$proto);
   $host = INADDR_ANY;
   $port = 8888;
   $address = pack_sockaddr_in($port, $host);
   bind(SOCK, $address);
   $queueSize = 5; # Queue up to 5 connections
   listen(SOCK, $queueSize);
   $hostName = inet_ntoa($host);
   print STDOUT "Server host: $hostName\n";
   print STDOUT "Server port: $port\n";
   $cAddress = accept(NEWSOCK,SOCK);
   ($cPort, $cHost) = unpack_sockaddr_in($cAddress);
   $cHostName = inet_ntoa($cHost);
   print STDOUT "Client host: $cHostName\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;

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