Perl | Socket Programming
Last Updated :
19 Jul, 2019
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:
Perl
#!/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);
Save the above script as
server_side.pl
Output:
Script to be run for Client creation:
Perl
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);
Save the above script as
Client_side.pl
Output:
Providing Input to test for Socket Programming:
Client End:
Server End:
Similar Reads
Socket Programming in C++
In C++, socket programming refers to the method of communication between two sockets on the network using a C++ program. We use the socket API to create a connection between the two programs running on the network, one of which receives the data by listening to the particular address port, and the o
5 min read
Perl | CGI Programming
In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests. It is a set of rules and standards that define how the information is exchanged between the web server and custom scripts. Earlier, scripting languages like Perl were used for writing the CGI applications. A
5 min read
Perl Programming Language
Perl is a general purpose, high level interpreted and dynamic programming language. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C, C++. Since Perl is a lot similar to other widely used lan
3 min read
Creating Unix Sockets
Sockets are a means to allow communication between two different processes over the same or different machines/networks. To be more precise, it's a way to talk to other computers and exchange data. In Unix, every I/O action is done by writing or reading a file descriptor. Sockets are the end point o
8 min read
Sockets | Python
What is socket? Sockets act as bidirectional communications channel where they are endpoints of it.sockets may communicate within the process, between different process and also process on different places.Socket Module- s.socket.socket(socket_family, socket_type, protocol=0) socket_family-AF_UNIX
3 min read
Python Falcon - Websocket
Websockets provide a powerful mechanism for establishing bidirectional communication between a client and a server over a single, long-lived connection. Python Falcon, a lightweight and fast web framework, offers support for implementing websockets, enabling real-time communication between clients a
3 min read
Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and
10 min read
Legacy Socket API in Java
The Java Socket API has been around for more than two decades. It has been maintained and updated over that period, but even the most well-kept code ultimately has to be upgraded to stay up with contemporary technologies. The fundamental classes that handle Socket interaction in Java 13 have been re
4 min read
Transport Layer Protocols
The transport layer is the fourth layer in the OSI model and the second layer in the TCP/IP model. The transport layer provides with end to end connection between the source and the destination and reliable delivery of the services. Therefore transport layer is known as the end-to-end layer. The tra
9 min read
WebSockets Protocol and Long Polling
As one facet of net development, real-time access Among consumers and servers has now become an important step in creating interactive web programs as well as mouth-watering. Request-reaction mechanisms, even flexible and powerful as they are, may not be suitable for circumstances that call for rapi
10 min read