Server - client IPC socket programming in Perl

walden systems, geeks corner, developer, perl, code, authentication, active directory, server, ipc, socket programming
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

Socket is a Berkeley UNIX mechanism of creating a virtual duplex connection between different processes. This was later ported on to every known OS enabling communication between systems across geographical location running on different OS software. If not for the socket, most of the network communication between systems would never exist. Taking a closer look; a typical computer system on a network receives and sends information as desired by the various applications running on it. This information is routed to the system, since a unique IP address is designated to it. On the system, this information is given to the relevant applications, which listen on different ports. For example an internet browser listens on port 80 for information received from the web server. Also we can write our custom applications which may listen and send/receive information on a specific port number.

For now, let's sum up that a socket is an IP address and a port, enabling connection to send and receive data over a network. To explain the socket concept we will take an example of Client - Server Programming using Perl. To complete a client server architecture we would have to go through the following steps:

To Create a Server

     - Create a socket using socket call. 
     - Bind the socket to a port address using bind call. 
     - Listen to the socket at the port address using listen call. 
     - Accept client connections using accept call. 



To Create a Client

     - Create a socket with socket call. 
     - Connect (the socket) to the server using connect call. 



The following is a Perl code for a simple client-server program using Perl socket. Here server listens for incoming requests and once connection is established, it replies "Hello World." The client reads that message and print on the screen. Server script

 1 #!/usr/bin/perl -w
 2 # Filename : server.pl
 3 
 4 use strict;
 5 use Socket;
 6 
 7 # use port 9000 as default
 8 my $port = shift || 90000;
 9 my $proto = getprotobyname('tcp');
10 my $server = "localhost";  # Host IP running the server
11 
12 # create a socket, make it reusable
13 socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
14    or die "Can't open socket $!
";
15 setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
16    or die "Can't set socket option to SO_REUSEADDR $!
";
17 
18 # bind to a port, then listen
19 bind( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
20    or die "Can't bind to port $port! 
";
21 
22 listen(SOCKET, 5) or die "listen: $!";
23 print "SERVER started on port $port
";
24 
25 # accepting a connection
26 my $client_addr;
27 while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
28    # send them a message, close connection
29    my $name = gethostbyaddr($client_addr, AF_INET );
30    print NEW_SOCKET "Hello World";
31    print "Connection received from $name
";
32    close NEW_SOCKET;
33 }

Client script

 1 !/usr/bin/perl -w
 2 # Filename : client.pl
 3 
 4 use strict;
 5 use Socket;
 6 
 7 # initialize host and port
 8 my $host = shift || 'localhost';
 9 my $port = shift || 7890;
10 my $server = "localhost";  # Host IP running the server
11 
12 # create the socket, connect to the port
13 socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
14    or die "Can't create a socket $!
";
15 connect( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
16    or die "Can't connect to port $port! 
";
17 
18 my $line;
19 while ($line = ) {
20    print "$line
";
21 }