Hi guys,

I am writing a udp client-server system and during the work I found a problem which I wanted to discuss with you. It is best explained through this snippet:

use strict; use IO::Socket; my $client = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => 3333, Proto => 'udp' ); (!$client) && die "failed to create sock: $!"; my ($request, $response); $request = 'sample req'; while(1 == 1) { if (! defined($client->send($request))) { print "send failed: $!\n"; } else { print "send succeeded\n"; } sleep 3; }

If there is no server on 3333, the output is pretty interesting:

send succeeded send failed: Connection refused send succeeded send failed: Connection refused . .

AFAIK, the reason why this would happen would be - in UDP the send system call cannot return the send status of the message as it is a blind throw. So if the message is within limits, the socket is okay, send returns true.

When the message is transmitted, if the host is not up we get a "connection refused" icmp error, which is captured in any of the sending/receiving operations I do later on and I get an error - the first send returned immediately after checking if the message can be sent. It can be. So after the first send and before the second, ICMP host not found was sent back. So the second send failed. Similarly it goes for the 3rd and 4th and so on.

My question:

How would you check the status of send sys call if you had to log an error if the host is down or something? (And no I don't want to use TCP :) ) - I was thinking of using Net::Ping module.

The output becomes all the more interesting if I configure a rule in iptables, telling it to drop icmp host unreachable responses. That way, the icmp response is dropped before it reaches my program and i get a "send succeeded" all the time, forever and ever, whether the host is up or down.


In reply to send call on udp socket by saurabh.hirani

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.