I'm not sure what you're trying to do here. You say you're trying to test the success of sending a UDP message, but you don't check the return value of your send. Even if you do check that, it's not going to tell you anything about the server on the other end. UDP is an unreliable, connectionless protocol -- you'll find out whether or not your bits went out on the wire, nothing more.

You're also doing strange things with the return value of select -- it returns an array of 3 array refs (read/write/error), or an empty array. In your code, the $read variable ends up with the length of the array in it, and you die if select returned an empty array. This works, but you'd probably be better off making $read an array, both for clarity and so you can do something meaningful with it if the select doesn't time out.

I'm not sure why you're getting the varying behavior -- on my box it's very consistent (select returns me a handle ready to read every time if I throw packets at an IP address with a live box on it; timeouts every time if it's a dead address). You are sending your packets to localhost, which may have something to do with it. If what you're trying to do is check for a response from the server you're throwing these packets at, you'll want to add a recv in there after the select to see what, if anything, the server sent back.

It's been a long time since I did anything with UDP, so I may be a little off here and there. You might find it enlightening to pick up a good TCP/IP networking book and read up on the guts of this stuff. Knowing what's supposed to happen behind the scenes makes this sort of debugging a lot easier.


In reply to Re: Socket response inconsistent by Cubes
in thread Socket response inconsistent by agoth

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.