Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Efficient non-blocking UDP server

by NetWallah (Abbot)
on Mar 19, 2004 at 21:40 UTC ( #338184=note: print w/replies, xml ) Need Help??

in reply to Efficient non-blocking UDP server

Take a look at the new Net::NBsocket module , which creates non-blocking sockets.

Here are some recent code snippents I wrote, that may be relevent as an alternative to NBsocket:

sub Rcv_Msgs{ my $handler_sub = shift; my $Echo_Request = shift; # Does he want us to Echo ? my @PortList = @{shift()} or die "Please specify one or more ports t +o listen on"; my @ExtraHandles = @{shift() || []}; # Existing handles to listen on +. my $Socket_Selector = IO::Select->new(); my @readyHandles; my %SockHandle_To_Port; $verbose and print "Setting up sockets to listen on ports @PortList\ +n"; # Setup all receive sockets and selector foreach my $port (@PortList){ my $handle = IO::Socket::INET->new( LocalPort => $port, Proto => "udp") or die "Couldn't be a udp Listener on port $port : $@\n"; $Socket_Selector->add($handle); $SockHandle_To_Port{$handle} = $port; # Next line required for RELAY operation... $Conversation{$port}{LISTENSOCKET} = $handle; $Conversation{$port}{FWDPACKETS} = 0; $Conversation{$port}{REPLYPACKETS} = 0; } # Throw in the STDIN ... ## $Socket_Selector->add(\*STDIN); # Does not work on Win32 # We also listen on the extra handles... foreach(@ExtraHandles){ $Socket_Selector->add($_); #Hmm - how do we associate these with what we sent ? } $verbose and print "Going into SERVER mode ==== loop receiving messa +ges:\n"; #while (my $Sender_Info = $rcv_socket->recv($datagram, $MAX_TO_READ, + $flags)) { while(@readyHandles = $Socket_Selector->can_read){ foreach my $h (@readyHandles){ # Find Port corresponding to handle.... my $rcvport = $SockHandle_To_Port{$h} || 0; my ( $Sender_addr, $Sender_port, $datagram) = read_socket($ +h); &$handler_sub($Sender_addr, $Sender_port, $datagram,$rcvport +); $Echo_Request and $h->send("ECHO:$datagram"); # ECHO it } } }
Offense, like beauty, is in the eye of the beholder, and a fantasy.
By guaranteeing freedom of expression, the First Amendment also guarntees offense.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://338184]
and a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-07-24 18:59 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (357 votes). Check out past polls.