Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Re: UDP and IO::Socket

by toadi (Chaplain)
on Jan 15, 2002 at 21:12 UTC ( #138955=note: print w/replies, xml ) Need Help??

in reply to Re: UDP and IO::Socket
in thread UDP and IO::Socket

It's not it doesn't start multiple instances when it's running. That I have solved. But when the client connects ten times, the 10 connects keep on waiting in the queue and my sleep will be run 10 times consecutive.

My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: Re: Re: UDP and IO::Socket
by agoth (Chaplain) on Jan 15, 2002 at 21:50 UTC
    Then you need to provide a slightly more complex server instance, that is a multiplexed reader. This will not wait for individual actions to complete and will allow you to close incoming reads if the sleep script is currently running yet respond to any read if sleep has finished?
    while (1) { ($read, $write, $err) = IO::Select->select($sockin, $sockout, $sock +err, 1); for $sock (@$read) { $sock->recv($data, 1024 * 64, 0); next if $! == EAGAIN() || $! == EWOULDBLOCK(); next if (-e ''); system ('./'); } }
      See where you are getting at, but the documentation if IO::Select is to sparse to understand your example. Can you give some more info?

      My opinions may have changed,
      but not the fact that I am right

        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 } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://138955]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2017-02-26 02:02 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (371 votes). Check out past polls.