As I understand it, (i.e. I'm not positive), calling IO::Select on a list of handles returns a list of waiting filehandles ( in this case sockets ), and in your case as you are not writing out, the only waiting handles will be readable handles, zero one or many.

In the loop, you can then iterate over any readable handles that have been assigned to @read by calling IO::Select, doing with as you wish. As you're using UDP, you don't need to worry about the connection unduly, nor have to close or give a response as the client doesn't wait for anything so you can just discard the client request as you don't want to sleep.

In the code below, the udp socket is added to the list of known waiting input readers,
IO::Select can accept many handles, the server enters an infinite loop, reading any waiting handles in and clearing the queue. It is important to get the non-blocking stuff in so that the server will skip over any hung items, though whether this non-blocking stuff actually works for UDP,( this was munged from a UDP/TCP combined server) and the IO::Select->select ( x,x,x, 1) enforces a 1 second sleep I think.

HTH, I only posted briefly yesterday 'cos I was in a rush, but hope this is clearer..

use POSIX; use IO::Socket; use IO::Select; my $udp = new IO::Socket::INET(LocalAddr => $interface,'LocalPort' +=> $port, 'Proto' => 'udp') or die $!; my $sockin = new IO::Select; my $sockout = new IO::Select; my $sockerr = new IO::Select; $sockin->add($udp); my ($read, $write, $err) = ([],[],[]); while(1) { ($read, $write, $err) = IO::Select->select($sockin, $sockout, $soc +kerr, 1); for my $sock (@$read) { my $data; $sock->recv($data, 1024 * 64, 0); next if $! == EAGAIN() || $! == EWOULDBLOCK(); #---- $! is po +pulated by IO::Socket # client request in $data ---- process as you want } for my $sock (@$write) { # not important } for my $sock (@$err) { # clean up any errors } }

In reply to Re: (5) : UDP and IO::Socket by agoth
in thread UDP and IO::Socket by toadi

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.