<?xml version="1.0" encoding="windows-1252"?>
<node id="1015031" title="Timeout not working while reading from socket." created="2013-01-23 16:45:34" updated="2013-01-23 16:45:34">
<type id="115">
perlquestion</type>
<author id="1013243">
jrtaylor</author>
<data>
<field name="doctext">
&lt;code&gt;

#!/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 =&gt; '192.168.173.9',
                              PeerPort =&gt; 7001,
                              Proto    =&gt; 'tcp',
			      Type     =&gt; SOCK_STREAM,
				);
die "cannot open socket" unless ($sock);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
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, "&gt;$filename" || die("Couldn't open $file");
$file-&gt;autoflush(1);

my $login = "&amp;NE014\r\n";
my $pass = "house\r\n";
my $dump = "P H 19\r\n";
my $logout = "&amp;NG\r\n";
my $enter = "\r\n";

print $sock $enter;
print $sock $enter;

#    if (&lt;$sock&gt; =~ m/^(R&gt; )/) {
#	print $sock $dump;
#    } else {
#	print $sock $logout;
#	print $sock $login;
#	print $sock $pass;
#	}

	print $sock $dump;

    while (my $line = &lt;$sock&gt;) {
	timeout $nb_secs =&gt; sub {
	my $count = 0;
	until ($count == 5 ) #Read the next xx lines
		{
		print $file $line;
		$sock-&gt;flush();
		$count++;
		}
	};
	if ($@){
        $line = "";
    }
}
close $file;
close $sock;

&lt;/code&gt;</field>
</data>
</node>
