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

i have a script used in my application to communicate with the application on a board and cater its requests over ethernet.
my script acts as client program, the link establishing code is as follows

sub tb_data_lnk_open { my ($link_add); my ($network_addr, $port_addr); my ($remote_host, $remote_port); $link_add = $_[0]; #===================================================================== +====# # The input $link_add contains the IP address and port in a single # # separated by a - # #===================================================================== +====# ($remote_host, $remote_port) = split( /-/, $link_add); #===================================================================== +====# # Create a socket. The socket is openend with TCP properties # #===================================================================== +====# socket(LINK_HNDL, PF_INET, SOCK_STREAM, getprotobyname('tcp')); #===================================================================== +====# # Remote address to connect in the network format # #===================================================================== +====# $network_addr = inet_aton($remote_host) or return 0; $port_addr = sockaddr_in($remote_port, $network_addr); #===================================================================== +====# # Connect to the server which is already listening # #===================================================================== +====# connect(LINK_HNDL, $port_addr) or return 1; return 0; }

and recieve function is as follows, which i want it to timeout after 100 seconds

sub tb_data_recv { my ($size, $data, $temp, $length_read); $size = $_[0]; $data = ""; $length_read = length($data); my $time_elapsed = 0; my $start_time = time(); #print "\nStart Time :$start_time "; my $count = 0; while($length_read < $size) { recv(LINK_HNDL, $temp, $size - length($data), $flags); $time_elapsed = time() - $start_time; #print "\n$count|Time Elapsed :$time_elapsed : $TIME_OUT : **$temp** : + **$data** \n"; if($time_elapsed > $TIME_OUT) { $data = ""; goto TEST_DOC_REP_CRASH; #return $data; } $data = $data.$temp; $length_read = length($data); $count++; } # print "\nTimeOut Not Detected"; return $data; }

but if i switch of board, where server is running, the perl script is still blocked at recv call.
can any one please tell me how to achieve that.(i have tried few things i found on internet, like setting timeout "setsockopt(LINK_HNDL, SOL_SOCKET, SO_RCVTIMEO, pack('LL', 1, 0));")
but still didn't work.
Script runs on windows machine, server/application(c code) runs on board on montavista linux.
if this is not the right place to ask, please give me pointers to where should i ask.
If anyother information is needed for understanding or solving, do tell me.

Replies are listed 'Best First'.
Re: problem with a blocking recv function over socket
by targetsmart (Curate) on May 12, 2009 at 08:52 UTC

      i have already tried the second function in the following manner

      # print "\nCalling recv()..."; my $select = IO::Select->new(); $select->add(LINK_HNDL); if($select->can_read(0)) { recv(LINK_HNDL, $temp, $size - length($data), $flags); #print "**$temp**\n"; } else { #print "No data on socket.\n"; }

      instead of normal recieve call, but result is the application/server on board is getting slower, even during a healthy run. timeout is getting detected.

      so what i want is, some way of setting recv so that it won't block when server is dead(not sure of this term,I mean switching off server(board on which it is running)abruptly).
        have you tried , MSG_DONTWAIT , do not block
        http://fuse4bsd.creo.hu/localcgi/man-cgi.cgi?recv+2

        Vivek
        -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.