Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Timeout not working while reading from socket.

by jrtaylor (Initiate)
on Jan 23, 2013 at 21:45 UTC ( #1015031=perlquestion: print w/replies, xml ) Need Help??
jrtaylor has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w use strict; use IO::Socket; use MIME::Lite; use Time::Out qw(timeout); use IO::Handle; $SIG{PIPE} = "IGNORE"; $| = 1; my $nb_secs = 10; my $buf = ""; my $file; umask 002; my $sock = new IO::Socket::INET (PeerAddr => '', PeerPort => 7001, Proto => 'tcp', Type => SOCK_STREAM, ); die "cannot open socket" unless ($sock); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); my $hrmin = sprintf ("%02d%02d",$hour,$min); my $ymd = sprintf("%04d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$mday,$ +hour,$min,$sec); my $filename = "/var/flexshare/shares/logs/backup.$ymd.txt"; open $file, ">$filename" || die("Couldn't open $file"); $file->autoflush(1); my $login = "&NE014\r\n"; my $pass = "house\r\n"; my $dump = "P H 19\r\n"; my $logout = "&NG\r\n"; my $enter = "\r\n"; print $sock $enter; print $sock $enter; # if (<$sock> =~ m/^(R> )/) { # print $sock $dump; # } else { # print $sock $logout; # print $sock $login; # print $sock $pass; # } print $sock $dump; while (my $line = <$sock>) { timeout $nb_secs => sub { my $count = 0; until ($count == 5 ) #Read the next xx lines { print $file $line; $sock->flush(); $count++; } }; if ($@){ $line = ""; } } close $file; close $sock;

Replies are listed 'Best First'.
Re: Timeout not working while reading from socket.
by Athanasius (Chancellor) on Jan 24, 2013 at 03:42 UTC

    Hello jrtaylor, and welcome to the Monastery!

    I can’t test your code, as Time::Out doesn’t work on Windows. But I’ve had a go at reducing your script to a minimum example (see How do I post a question effectively?):

    #! perl use strict; use warnings; use IO::Socket; use Time::Out qw(timeout); $SIG{PIPE} = "IGNORE"; $| = 1; umask 002; my $socket = new IO::Socket::INET( PeerAddr => '', PeerPort => 7001, Proto => 'tcp', Type => SOCK_STREAM, ) or die "Can't open socket: $@\n"; # Open the output file my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time); my $ymd = sprintf "%04d%02d%02d%02d%02d%02d", $year + 1900, $mon + 1, $mday, $hour, $min, $sec; my $file = "/var/flexshare/shares/logs/backup.$ymd.txt"; open(my $out, '>', $file) or die "Can't open file '$file' for writing: + $!"; $out->autoflush(1); # Send to the socket print $socket "\r\n\r\nP H 19\r\n"; # Read from the socket my $nb_secs = 10; while (my $line = <$socket>) { timeout $nb_secs => sub { my $count = 0; until ($count == 5) # Read the next xx lines { print $out $line; $socket->flush(); $count++; } }; if ($@) { $line = ""; } } close $out; close $socket;

    As far as I can see, the code within timeout $nbsecs => sub { ... } does nothing but write the same $line to the output file 5 times, flushing the socket each time. Did you mean to put the while loop that reads from the socket into the timeout sub?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      I see what I did wrong..... THANKS. This seems to work.
      while (my $line = <$sock>) { if ($line =~ m/^(ALARM: )/) { timeout $nb_secs => sub { while (2) { $buf .= $sock->getline(); $sock->flush(); } }; if ($@) { my $msg = MIME::Lite->new( ....... Type =>'TEXT', Data =>$buf , ); $msg->send(); print $file $buf; $line = ""; $buf = ""; } } print $file $line; #Print current line to open $file. } } sleep 10; # allow sometime to exit close $file;
Re: Timeout not working while reading from socket.
by zentara (Archbishop) on Jan 24, 2013 at 10:32 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015031]
Approved by Corion
[holli]: "die Kleine" is a (somewhat diminishing) term for woman :) also someone at stock overflow seems to think i'm female
[Discipulus]: holli orange is gold in the morning, silver in afternoon and lead in the nigth..
[Discipulus]: banana too better not in the night
[holli]: it actually feels more like a lead-acid battery ^^
[Discipulus]: erix are you now travelling in space? iirc you were in low countries
choroba had 4 beers (or was it 5?) yesterday, plus a pickled cheese
[choroba]: megalag :-(
[erix]: ok, I looked it up, should have been: "Haribo macht Kinder froh und Erwachsene ebenso"
[Discipulus]: also mixing pizza (fermented/levited ) with juices is not so good

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-11-22 09:05 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (316 votes). Check out past polls.