in reply to Re^2: Advice on perl socket activity
in thread Advice on perl socket activity

I had no trouble getting the timeout to work properly. The connection does not get dropped. Either get() or getline() will work but I settled on get().

#!/usr/bin/perl use strict; use warnings; use diagnostics; use IO::Handle; use Net::Telnet; my ($host, $username, $passwd) = ("localhost", "issac", "YYYYY"); autoflush STDOUT 1; my $t = Net::Telnet->new(); # Log into unix box print localtime().": Connect to $host..."; my $ok = $t->open(Host => $host, Timeout => 20, Errmode => "return"); die ("Cannot connect to $host telnet server") if !$ok; print "OK\n"; print localtime().": Login to $host..."; $ok = $t->login(Name => $username, Password => $passwd, Timeout => 20, Errmode => "return"); die ("Cannot login to $host telnet server") if !$ok; print "OK\n"; # Read from server continually. # Periodically send text to server. while (1) { my $output = $t->get(Timeout => 10, Errmode => "return"); if (defined $output) { # Recieved text from server print localtime().": get() returned definite output=\"$output\ +"\n"; } elsif ($t->eof()) { # End of file - server has hung up. print localtime().": get() returned EOF\n"; last; } else { # Timeout occured print localtime().": get() returned TIMEOUT\n"; print localtime().": put() sending string\n"; # Send some crap to the server my $ok = $t->put(String => "echo HELLO\n", Timeout => 10, Errmode => "return"); if (!$ok) { if ($t->eof()) { print localtime().": put() returned EOF\n"; last; } else { print localtime().": put() returned TIMEOUT\n"; } } } }

Here is some sample output:

gmargo@tesla 1753$ perl telnet_test.pl Thu Nov 5 19:41:00 2009: Connect to localhost...OK Thu Nov 5 19:41:00 2009: Login to localhost...OK Thu Nov 5 19:41:11 2009: get() returned TIMEOUT Thu Nov 5 19:41:11 2009: put() sending string Thu Nov 5 19:41:11 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:11 2009: get() returned definite output="HELLO issac@tesla 79$ " Thu Nov 5 19:41:22 2009: get() returned TIMEOUT Thu Nov 5 19:41:22 2009: put() sending string Thu Nov 5 19:41:22 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:22 2009: get() returned definite output="HELLO issac@tesla 80$ " Thu Nov 5 19:41:33 2009: get() returned TIMEOUT Thu Nov 5 19:41:33 2009: put() sending string Thu Nov 5 19:41:33 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:33 2009: get() returned definite output="HELLO issac@tesla 81$ " Thu Nov 5 19:41:44 2009: get() returned TIMEOUT Thu Nov 5 19:41:44 2009: put() sending string Thu Nov 5 19:41:44 2009: get() returned definite output="echo HELLO " Thu Nov 5 19:41:44 2009: get() returned definite output="HELLO issac@tesla 82$ "

Replies are listed 'Best First'.
Re^4: Advice on perl socket activity
by sans-clue (Beadle) on Nov 06, 2009 at 14:16 UTC
    my $ok = $t->put(String => "echo HELLO\n", Timeout => 10, Errmode => "return");
    very nice code. If it took 3 secs for the echo HELLO to complete, and in that 3 secs there was output, wouldn't the output get dropped ? Thanks update: I think I have something workable, using print instead of put, I am not missing anything yet. Print has no timeout. Thanks for your prodding.

      No. The socket connection takes care of that. You'll get() it on the next loop.