HomeAboutMailing ListList Chatter /0/0 3.230.143.213

RE: Perl TCP

2023-10-19 by: don@coastlandtech.co
From: don@coastlandtech.com
------------------------------------------------------

#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::INET;
use IO::Select;

# Create a listening socket
my $server =3D IO::Socket::INET->new(
    LocalPort =3D> 12345,
    Type      =3D> SOCK_STREAM,
    Reuse     =3D> 1,
    Listen    =3D> 5
) or die "Cannot create socket: $!";

print "Server listening on port 12345...\n";

# Use IO::Select to handle multiple connections
my $selector =3D IO::Select->new($server);

while (1) {
    # Check sockets ready for reading
    my @ready =3D $selector->can_read;
   =20
    foreach my $socket (@ready) {
        # If the socket is the server socket, accept the new connection
        if ($socket =3D=3D $server) {
            my $new_socket =3D $server->accept;
            $selector->add($new_socket);
        } else {
            # Read data from the socket
            my $data;
            $socket->recv($data, 1024);
           =20
            # Remove connection if it's closed or empty
            unless ($data) {
                $selector->remove($socket);
                next;
            }
           =20
            # Respond based on received data
            my $response =3D get_response($data);
            $socket->send($response);
           =20
            # Close the socket
            $selector->remove($socket);
            $socket->close;
        }
    }
}

sub get_response {
    my $input =3D shift;
   =20
    if ($input =3D~ /^1\s*$/) {
        return "2";
    } elsif ($input =3D~ /^2\s*$/) {
        return "5";
    } else {
        return "Invalid input";
    }
}


Maybe start here ... =F0=9F=98=8A=20



-----Original Message-----
From: Unkmar =20
Sent: Wednesday, October 18, 2023 8:05 PM
To: don@coastlandtech.com
Subject: [chugalug] Perl TCP


I want to ask for help. Because that is the working smarter not harder.
I don't want to ask for help. Because I don't want to be a bother to =
someone that doesn't just inherently know how to do it with almost no =
effort. I mean, I can figure it out without help. It will just likely =
take me a few hours (6+), while someone that "knows" can likely whip a =
sample up in 30 minutes to an 1 hour.

The task is a Perl TCP listener (server), that can dynamically respond =
to the input.
Overly simple example. If it gets "1" it can respond with "2". If it =
gets "2" it responds with "5". The data transfer in or out will be less =
than 1Kb, less than 1024 bytes.
I need it to capable of async or non-blocking. So that a connection =
remaining open doesn't prevent second connection from succeeding. I also =
prefer all the code to be in a single file, and that is only because I =
believe it should be less than 200 lines. Probably around
100 lines or less.

Meanwhile, I will go back to other things for now. And later begin =
burning a few hours trying to cobble something together.

-- Lucius L. Hilley III