How to write simple TCP server and client in Perl

Last updated on October 15, 2020 by Dan Nanni

If you want to create a server and client which communicate with each other via TCP, there are many ways to implement it. In this post, I will show you a simple TCP server and client example written in Perl.

In this example, a client connects to a server with well-known IP address and port. Once the client is connected to the server, it sends data to the server, and waits for a reply. Upon receiving data from the client, the server responds with an acknowledgement message.

In the example code below, I am using IO::Socket::INET which provides an interface for creating and using AF_INET sockets. This module should be pre-installed on Linux.

In the code, the purpose of shutdown(SOCKET, 1) is to tell the other end point that you have finished sending data, without closing the socket. Note that you can still receive data from the socket afterward.

TCP Server Example in Perl

use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# creating a listening socket
my $socket = new IO::Socket::INET (
    LocalHost => '0.0.0.0',
    LocalPort => '7777',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
);
die "cannot create socket $!n" unless $socket;
print "server waiting for client connection on port 7777n";

while(1)
{
    # waiting for a new client connection
    my $client_socket = $socket->accept();

    # get information about a newly connected client
    my $client_address = $client_socket->peerhost();
    my $client_port = $client_socket->peerport();
    print "connection from $client_address:$client_portn";

    # read up to 1024 characters from the connected client
    my $data = "";
    $client_socket->recv($data, 1024);
    print "received data: $datan";

    # write response data to the connected client
    $data = "ok";
    $client_socket->send($data);

    # notify client that response has been sent
    shutdown($client_socket, 1);
}

$socket->close();

TCP Client Example in Perl

use IO::Socket::INET;

# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
    PeerHost => '192.168.1.10',
    PeerPort => '7777',
    Proto => 'tcp',
);
die "cannot connect to the server $!n" unless $socket;
print "connected to the servern";

# data to send to a server
my $req = 'hello world';
my $size = $socket->send($req);
print "sent data of length $sizen";

# notify server that request has been sent
shutdown($socket, 1);

# receive a response of up to 1024 characters from server
my $response = "";
$socket->recv($response, 1024);
print "received response: $responsen";

$socket->close();

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean