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.