Hi kroach

What you're looking for is IO::Socket::Timeout.

See below for a bastardised version of your code that uses it (change $CLIENT_WAIT to 2 and you'll see the server response).

#!/usr/bin/perl -s use strict; use warnings FATAL => 'all'; use Errno qw(ETIMEDOUT EWOULDBLOCK); use IO::Socket::Timeout qw(IO::Socket::UNIX); use IO::Socket::UNIX; use Sys::Hostname; # treat uppercase variables as constants my $SOCK_PATH = "$ENV{HOME}/test_socket.sock"; my $SERVER_WAIT = 3; my $CLIENT_WAIT = 4; # program switches [see '-s' perl command line option] our ($s,$c); die usage() if (defined $s && defined $c) || (!defined $s && !defined $c); # server: if ($s) { unlink $SOCK_PATH; my $server = IO::Socket::UNIX->new( Type => SOCK_STREAM(), Local => $SOCK_PATH, Listen => 1, ); my ($count,$hostname) = 1; while (my $conn = $server->accept()) { $conn->print(job($count)." Hello from ".hostname ." @ ".localtime."\n"); } } # client: elsif ($c) { my $client = IO::Socket::UNIX->new( Type => SOCK_STREAM(), Peer => $SOCK_PATH, ); # IO::Socket provides a way to set a timeout on the socket, but # the timeout will be used only for connection, not for reading # / writing operations. So we use IO::Socket::Timeout :-) IO::Socket::Timeout->enable_timeouts_on($client); $client->read_timeout($CLIENT_WAIT); # evaluate timeout my $response = <$client>; if (! $response && ( 0+$! == ETIMEDOUT || 0+$! == EWOULDBLOCK )) { die "timeout reading socket; committing seppuku"; } print "server says: ". $response ."\n"; } # unrecognised mode: else { die "see usage instructions; unrecognised operating mode"; } sub usage { return "\nusage: perl $0 <switch>\n\n" ."<Switches> - no clustering; single switch use only\n" ." -s : run $0 in server mode\n" ." -c : run $0 in client mode\n"; } __END__

Good luck,
Shadow


In reply to Re: Making IO::Socket::UNIX client time out by shadowsong
in thread Making IO::Socket::UNIX client time out by kroach

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.