Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3945700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:02:47+00:00 2026-05-20T01:02:47+00:00

I have an application that makes connections to various equipment in our network on

  • 0

I have an application that makes connections to various equipment in our network on demand, performs several commands, parses the output and reports back to the user via AJAX/JSON/Perl. In Perl, I am using NET::Telnet / ::Cisco, along with the occasional SSH connection via spawning a child process and passing that to NET::Telnet.

I am looking to enhance the application by creating a sort of telnet session holder that will maintain connections after they are opened for a set period of time, or until they time out. The idea is to cut down on the reconnecting to these devices, and allowing other requests to use a telnet session that was created before (provided it’s not in use and is still active).

I started writing a perl file using IO::Socket::UNIX, and am able to store the connections w/o issue, and basically another file will use the socket created to access or create new connections. The problem I am having is this: If two requests hit that socket at the same time, which ever one came in first will cause the second one to have to wait for the first to be done processing.

I started experimenting with using threads, but if I do that, I can’t pass the NET::Telnet object back to the original/parent thread.

Does anyone have any ideas on how to accomplish this? Is there maybe an application that will act as a session holder that I can interface with?

UPDATE

I used POE per the suggestion from one commenter, and while this does partially what I’m looking for, it does not allow the ‘server’ to service multiple connections simultaneously.

Scenario:
Two users click ‘send’ on the front end rather close together. User A’s query reaches the server first, but they are trying to connect to a device that takes a long time to respond. Thusly, User B will have to wait until User A’s connection is established before User B’s request is even started. Essentially

, I need to be able to service simultaneous requests using a connection pool without creating delay for anyone else just because the guy that got there first is trying to connect to that slooooow device..

Below is my code which, if this is how I would do it, will be run in the background. The prints are for my debugging.

#!/usr/bin/perl -w

use strict;
use JSON;
use Net::Telnet;
use IO::Socket::UNIX qw( SOCK_STREAM SOMAXCONN );

my $socketPath = '/tmp/telnetproxy';
unlink($socketPath);

my $listener = IO::Socket::UNIX->new(
    Type   => SOCK_STREAM,
    Local  => $socketPath,
    Listen => SOMAXCONN) 
    or die ("Cannot create server socket: $!\n");

my $clientNum = 0;

our $json = JSON->new->allow_nonref;
our $conns = {};

print "Server Initiated...\n";

while (1) {

    print " - Inside while loop...\n";

    my $socket = $listener->accept();

    connectToDevice(++ $clientNum, $socket);

}

sub connectToDevice {
    my $connectionNum = shift;
    my $socket = shift;

    print " - Inside 'connectToDevice'\n";

    print " - Connection #$connectionNum started...\n";

    my $input;
    my $connId = 0;
    my (@argsRaw, $args, @argHold);
    my $numOfConnections = keys %$conns;

    my $deviceProperties = {
        ipAddress => undef,
        username  => undef,
        password  => undef,
        method    => 'telnet'
    };

    print " - waiting for input...\n";

    # Receive input for arguments.
    chomp( $input = <$socket> );

    print " - input received...\n";

    ## Turn string into a HASHREF
    $args = from_json($input);

    foreach (keys %$args) {
        print "\t$connectionNum: $_ => $args->{$_}\n";
        if (/^host$/i) { #---- Host IP given ($self->{_hostIp{
            if (verifyIp($args->{$_})) {
                $deviceProperties->{ipAddress} = $args->{$_};
            } else {

            }
        }
         elsif (/^method$/i) { # Ckt type... very important for how we ts
            $deviceProperties->{method} = $args->{$_};
        }
         elsif (/^(username|user|u)$/i) { # username to log in with
            $deviceProperties->{username} = $args->{$_};
        }
         elsif (/^(password|pass|p)$/i) { # password
            $deviceProperties->{password} = $args->{$_};
        }
    }

    print " - Num of connections: $numOfConnections\n";

    if ($numOfConnections > 0) {
        ## Look through existing connections
        ##  1) If we have an available connection, use it
        ##  2) If not, create a new connection.
        print " - Checking existing connections...\n";
        foreach my $connKey ( keys %$conns ) {
            if ($conns->{$connKey}->{host} eq $deviceProperties->{ipAddress} && $conns->{$connKey}->{locked} == 0 && testConnection($connKey)) {
                $connId = $connKey;
                print "\tconnection #$connKey... VALID, using it\n";
                last;
            } else {
                print "\tconnection #$connKey... not valid\n";
            }
        }
    } else {
        print " - No existing connections, creating a new one ...\n";
    }

    if ($connId == 0) {

        $connId = $connectionNum;

        $conns->{$connectionNum} = {
            host   => $deviceProperties->{ipAddress},
            locked => 1
        };

        $conns->{$connectionNum}->{conn} = connectToHost($deviceProperties, "blab_$connectionNum");
        print " - Created a new connection, a suitable existing connection was not found.\n";
    }

    print " - Waiting for command.. ";

    chomp( my $line = <$socket> );

    print "DONE\n";

    my @out = $conns->{$connId}->{conn}->cmd($line);

    print " - Sent '$line' to device\n";

    my $numOfLines = @out;

    print " - $numOfLines lines retrieved\n";

    $conns->{$connId}->{locked} = 0;

    print " - This run done....\n\n";

    return;
}

sub testConnection {
    my $connectionNum = shift;

    print " -- Testing connection $connectionNum:  ";

    my @out = $conns->{$connectionNum}->{conn}->cmd(String => '!', Timeout => 2);

    print "[";
    print @out;
    print "]";

    if (@out > 0) {
        print " ---- Good\n";
        return 1;
    } else {
        delete $conns->{$connectionNum};
        print " ---- No good\n";
        return 0;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:02:47+00:00Added an answer on May 20, 2026 at 1:02 am

    I have moved to using POE server architecture, along with spawning sub/child-processes to handle individual connections. It’s probably not the prettiest/best/most efficient way to do what I want, but it gets the job done and has a vary narrow purpose.

    Thanks everyone to all their help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that makes a couple hundred TCP connections at the same
I have a Win32 application that makes ODBC-Connections. We connect using SQLDriverConnect() which displays
I have written java application that makes socket connections with a legacy system. How
I have application that makes different queries with different results so the caching in
I have C# application that makes use of some C libaries(which I have written
I have an application that makes use of frequently updated lists. So for example,
So we have an application that makes udp calls and sends packets. However, since
I have a Java application that makes heavy use of a large file, to
I have a multithreaded application that makes heavy use of OpenSSL in C. It
I have a WinForms application that makes use of a TaskDialog library that leverages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.