in reply to Timeouts when reading from socket Filehandles?
"The creativity is the expression of the liberty".#!/usr/bin/perl ## Open the 2 socket connections: my $sock1 = new IO::Socket::INET( PeerAddr => host1 , PeerPort => 6622 , Proto => 'tcp', Timeout => 30) ; my $sock2 = new IO::Socket::INET( PeerAddr => host2 , PeerPort => 6622 , Proto => 'tcp', Timeout => 30) ; ## Crete the interface for select, with the 2 sockets: my $can_read = IO::Select->new( $sock1 , $sock2 ); ## File to log the data: open (LOG,"./log.txt") ; ## Here the select interface will return to the array ## @has_buf the $sockets with data in the buffer, using ## 1s of delay: while( my @has_buf = $can_read->can_read(1) ) { foreach my $has_buf_i ( @has_buf ) { ## $has_buf_i is one of the + sockets my $buffer ; ## Create another select to can read byte by byte, because ## we just know that the socket have data, but not the length: my $sel = IO::Select->new($has_buf_i) ; ## Read byte by byte: while( $sel->can_read(0.1) ) { sysread($has_buf_i, $buffer , 1 , length($buffer) ) ; } ## Print to the log file: if ($buffer ne '') { print LOG $buffer ;} } } exit;
|
|---|