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
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
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
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
Python - Simple Port Scanner with Sockets Ports can be scanned to check which ports are engaged and which ports are open or free. In Python "Socket" module provides access to the BSD socket interface, which is available on all platforms. To scan the ports, the following steps can be implemented: 1] Recognize the host's IP address 2] Create
2 min read
Java ServerSocket Class ServerSocket Class in Java provides a system-independent way to implement the server side of a client/server socket connection. The constructor for ServerSocket throws an exception if it canât listen on the specified port (for example, the port is already being used).In the java.nio channel, ServerS
4 min read
Types of Socket A network that is connected with two devices as a link to execute two-way communication on the network. It receives and sends data to the devices. The socket address is a combination of IP address and port. In the TCP/IP layer, a socket is bound as a port number which can identify whether the data i
3 min read
java.net.Socket Class in Java The java.net.Socket class allows us to create socket objects that help us in implementing all fundamental socket operations. We can perform various networking operations such as sending, reading data and closing connections. Each Socket object that has been created using with java.net.Socket class h
5 min read