- The Socket Module

This section provides a tutorial example on how to use the 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("") - Returns the packed form of the specified IP address, similar to pack('C4', 216,109,118,67). inet_aton("") 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:

#- Copyright (c) All Rights Reserved.
   use Socket;
   $proto = getprotobyname('tcp');
   $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";

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