bengmau has asked for the wisdom of the Perl Monks concerning the following question:

I need to create a connection to a destination address on port xx via TCP. Subsequently I want to continously read in the input and look for a particular regex and act when I see it. Should I simply just use the Net::Telnet open a session and wrap a getline in a while loop?
my $session = new Net::Telnet (Timeout => 30); $session->open(Host => $host, Port => $port); while ($session) { my $line; $line=$session->getline(); .... }
or should I use something else like IO::Socket::Inet? recommendations?

Replies are listed 'Best First'.
Re: use telnet to connect to tcp session and receive data
by Joost (Canon) on Jun 22, 2005 at 14:40 UTC
Re: use telnet to connect to tcp session and receive data
by dave0 (Friar) on Jun 22, 2005 at 14:30 UTC
    Unless you're connecting to an actual Telnet server, you should probably use IO::Socket::INET.

      Not really. From the Net::Telnet:

      Other reasons to use this module than strictly with a TEL­NET port are:
      
        · You're not familiar with sockets and you want a simple way to make client connections to TCP services.
        
        · You want to be able to specify your own time-out while connecting, reading, or writing.
      
        · You're communicating with an interactive program at the other end of some socket or pipe and you want to wait for certain patterns to appear.
      

      When debugging, I use the command line telnet to connect to all types of servers (http, smtp, etc). There's no reason why you cannot use Net::Telnet the same way.

      -derby
        yea.. I figured.. either works however when I use a simple inet socket connection there's a lot of garbage in front of the text which leads me to believe there are format codes in the message. Leads me to think it's in vt100 mode or something. I guess I may have to use net::telnet after all
Re: use telnet to connect to tcp session and receive data
by zentara (Cardinal) on Jun 22, 2005 at 19:12 UTC
    Here is a general purpose interactive socket client that I use for testing, it probably could be made to do your business.
    #!/usr/bin/perl -w use strict; use IO::Socket; my ( $host, $port, $kidpid, $handle, $line ); #unless ( @ARGV == 2 ) { die "usage: $0 host port" } ( $host, $port ) = @ARGV || ('localhost',4444); # create a tcp connection to the specified host and port $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) or die "can't connect to port $port on $host: $!"; $handle->autoflush(1); # so output gets there right away print STDERR "[Connected to $host:$port]\n"; # split the program into two processes, identical twins die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process if ($kidpid) { # copy the socket to standard output while ( defined( $line = <$handle> ) ) { print STDOUT $line; } kill( "TERM", $kidpid ); # send SIGTERM to child } # the else{} block runs only in the child process else { # copy standard input to the socket while ( defined( $line = <STDIN> ) ) { print $handle $line; } }

    I'm not really a human, but I play one on earth. flash japh
Re: use telnet to connect to tcp session and receive data
by Anonymous Monk on Jun 23, 2005 at 19:21 UTC
    old school: http://www.cpan.org/scripts/netstuff/sock.pl had lots of fun with that as the core of many tcp port playthings.