How to write simple TCP server and client in Perl

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, client connects to a server with well-known IP address and port. Once client is connected to the server, it sends data to the server, and waits for a reply. Upon receiving data from client, server responds with an acknowledgement message.

The purpose of shutdown(SOCKET, 1) in the example is to tell the other end that you have finished sending data, without closing SOCKET. Note that you can still receive data 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 7777\n";

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_port\n";

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

    # 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 server\n";

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

# 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: $response\n";

$socket->close();

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

4 thoughts on “How to write simple TCP server and client in Perl

  1. Any way we can do in python?
    I was thinking of setting up a jabber server for communication! Is that a bad idea?

    • I meant I earlier thought that setting up an xmpp communication is a way to communicate messages from sever to a client!
      This script does an awesome job of doing so. Just had to ask if the same can be done in python.
      Sure the script gets the work done! But there are things like encrypting it and then decrypting it on the other side. I don't mean some obvious options like add 1 char to the words and send, then remove the same. Maybe something of ssh kind of security. Just gave a thought! Though much useless :O

      :D

      • Sure, writing such server/client in python should be straightforward. I guess it's for another post. :-)

        As for message encryption, it's easy. Just use IO::Socket::SSL, instead of plain socket as in the example.

  2. Can any one help me regarding perl socket creation, and sending an XMl request (header and body) and reading response.

    Thanks in advance

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *