Perl | Socket Programming

Socket programming in Perl is a way of connecting two nodes on a network to communicate with each other. Basically, it is a one-way Client and Server setup where a Client connects, sends messages to the server and the server shows them using socket connection. One socket (node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. Before going deeper into Server and Client code, it is strongly recommended to go through TCP/IP Model.

Any computer system that is on a network needs a local port to transfer data over the internet. This data is directed to the designated system with the use of it’s IP Address. Each and every system receives and sends this data over the network by the ports designated for the respective process. For example, port 80 is the default port to receive information sent from the server. This port needs not to be same always, it can be decided by the user as well.
Therefore, a socket is an IP address and a port together, which enables the network connection to send/receive information to other networks or systems.

Socket Programming can be better understood by creating a Server and a client on two different consoles by running Perl scripts and then transferring data between both via a secure connection.




Above image illustrates the calls that are must for running server side and client side scripts in Socket programming.

Stages for Server-side Programming

To create a server in socket programming, the following stages are performed step by step:
 

-> Creating a socket using the socket() call function. Perl provides a predefined module Socket.pm which needs to be included in the code using ‘use‘ pragma.

use Socket;

This module will help creation of a socket at the server end

-> bind() call is used to bind the socket with a port number. Without a port, the socket will be of no use. The server uses this bind() function to assign a port to a socket.

bind(socket, port_address)

-> listen() call is to enable the port to wait for any incoming requests. This call is done by the server to provide a limit of the connection requests allowed with the server.

listen(socket, size)

Here, size is used to pass request limit.


-> accept() call is used to issue a request to the access() function to accept the incoming connections.

accept(new_socket, socket)

If the access() call is successful then a new socket is returned for future connections with the respective client.

Stages for Client-side Programming

To create a client in socket programming, the following stages are performed step by step:
 

-> Creating a socket using the socket() call function. Perl provides a predefined module Socket.pm which needs to be included in the code using ‘use‘ pragma.

use Socket;

This module will help creation of a socket at the client end

-> connect() call is used to connect the socket with the server by using a specific address.

connect(socket, address)

Here, address is similar to as in bind() call except that it contains the IP address of the remote server.


Socket Programming Example:

Script to be run for Server creation:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl -w  
use IO::Socket;   
use strict;   
use warnings;   
  
my $socket = new IO::Socket::INET (   
LocalHost => 'localhost',   
LocalPort => '6666',   
Proto => 'tcp',   
Listen => 1,   
Reuse => 1,   
);   
  
die "Could not create socket: $!n" unless $socket;   
  
print "Waiting for data from the client end\n";   
my $new_socket = $socket->accept();   
while(<$new_socket>) 
{   
    print $_;   
}   
  
close($socket);  

chevron_right


Save the above script as server_side.pl
Output:

Script to be run for Client creation:

filter_none

edit
close

play_arrow

link
brightness_4
code

use strict;   
use warnings;   
use IO::Socket;   
  
my $socket = new IO::Socket::INET (   
PeerAddr => 'localhost',   
PeerPort => '6666',   
Proto => 'tcp',   
);   
  
die "Could not create socket: $!n" unless $socket;
  
print "Enter data to send:\n";  
my $data = <STDIN>;  
chomp $data;  
print $socket "Data received from user: '$data'\n";  
  
close($socket);  

chevron_right


Save the above script as Client_side.pl
Output:

Providing Input to test for Socket Programming:

Client End:

Server End:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.