Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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.


Comment on Re: Efficient non-blocking UDP server
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://338184]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2015-07-05 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls