For some reason, this website has been having trouble staying up. But it is working at the moment. I will attach a client/server pair that I wrote some time ago. This shows one way of using the alarm signal to timeout read requests to the server. The big problem with this approach is that once you get a signal, there is not much you can do about it other than clean up some stuff and die. But often that is enough. See if this approach will work for you.

There are other ways where it is possible to check if data is available on the pipe before attempting to read so that you don't hang. I have another project competing for my time right now. So can't provide an example of that right away.

#!/usr/bin/perl # Simple client for testing # https://www.perlmonks.org/?node_id=11151665 use strict; use warnings; $!=1; use IO::Socket; my $socket = IO::Socket::INET -> new (PeerAddr => 'localhost', PeerPort => 8081, Proto => 'tcp', Type => SOCK_STREAM) or die "Cannot open socket!"; print "$socket"; my $cmd = <<'END_MESSAGE'; GET /DATA_String/{300 continuous characters of sensor data}HTTP/1.1 Some host line Some Connection line Some user-agent line, next line is blank END_MESSAGE $SIG{PIPE} = 'handler'; $SIG{INT} = 'handler'; $SIG{QUIT} = 'handler'; $SIG{ALRM} = 'handler'; server_request($cmd); #single request close $socket; #hang up exit(); sub server_request { my $cmd = shift; print "sending CMD to server\n"; alarm(2); print $socket "$cmd"; $socket->flush(); alarm(0); print "client debug: back from server: \n"; while (alarm(2),my $sResponse = <$socket>) { alarm(0); print "from Server: $sResponse"; } alarm(0); } sub handler { my ($signo) = shift; if ($signo eq "INT" or $signo eq "QUIT") { print $socket "QUIT\n"; close ($socket); exit (1); } elsif ($signo eq "ALRM") { print "server too slow - request timed out!\n"; exit (2); } else #SIGPIPE (probably!) { print "Server died with signal $signo\n"; exit (3); } }
#!/usr/bin/perl # Perl SOPW # https://www.perlmonks.org/?node_id=11151665 use strict; use warnings; $|=1; #Turn stdio buffering off use IO::Socket; use POSIX qw(sys_wait_h); use constant LISTEN_PORT => 8081; my $socket = IO::Socket::INET->new ( LocalPort => 8081, Proto => 'tcp', Type => SOCK_STREAM, Reuse => 1, Listen => 10, ) or die "Could not open port 8081!"; my $client; while (1) { $client = $socket->accept() || next; #accept() is not re-entrant #loop if error my $pid = fork(); die "Bad Fork!" unless defined $pid; if ($pid == 0 ) # child { close ($socket); # children don't listen for new connections my $line; my @buff; while (defined ($line=<$client>) and $line !~ /^\s*$/) { push @buff, $line; } my $time = time(); print "INCOMING from a Client Connection at $time\n"; do_command(\@buff); exit(0); } else # parent { close ($client); #wait for next client request } } sub do_command { my $buff = shift; print $client "START\n"; print $client "$_" for @$buff; #just loopback lines to client print $client "END\n"; $client->flush(); }

In reply to Re: Socket read timeout on Windows by Marshall
in thread Socket read timeout on Windows by bonzi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.