I'm working on a little client/server application. The server does not constantly have a socket open. At regular intervals it opens a socket and listens for a request. Thanks to some advice I got here before, I'm using IO::Select so that I don't block on that socket.

My problem now is that occasionally, when the client sends its udp request, the server does not have the socket open, and an ICMP port unreachable message is sent back to the client. The client dies at this point.

What I want to happen is for the client to resend the request. I'll include the code below.

#!/usr/bin/perl use IO::Socket; foreach $request (@ARGV) { $MAXLEN = 1024; $PORTNO = 5151; $TIMEOUT = 5; #$request = "198.59.115.12:25"; $server_host = "kona.swcp.com"; $sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => $PORTNO, PeerAddr => $server_host) or die "Creating socket: $!\n"; $sock->send($request) or die "send: $!"; eval { local $SIG{ALRM} = sub { die "alarm time out" }; alarm $TIMEOUT; $sock->recv($reply, $MAXLEN) or die "recv: $!"; alarm 0; 1; } or die "recv from $server_host timed out after $TIMEOUT seconds.\n"; print $reply; sleep 5; }

In reply to don't die, retry by Anonymous Monk

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.