Created
January 13, 2013 11:12
-
-
Save cosimo/4523568 to your computer and use it in GitHub Desktop.
Simple AnyEvent UDP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
=head1 NAME | |
anyevent-udp-server.pl - Simple AnyEvent::Handle::UDP UDP server | |
=head1 DESCRIPTION | |
Starts a UDP server that listens on a given port for | |
input (incoming udp packets). | |
To test it, you can use the following command: | |
$ echo "Test1" | nc -u -q1 localhost 4000 | |
=cut | |
use 5.010; | |
use strict; | |
use warnings; | |
use AnyEvent::Handle::UDP; | |
use AnyEvent::Log (); | |
# Default for AnyEvent is to log nothing | |
$AnyEvent::Log::FILTER->level('debug'); | |
# AE::Handle::UDP does all for us: | |
# be sure to use the "bind" option! | |
my $udp_server = AnyEvent::Handle::UDP->new( | |
# Bind to this host and port | |
bind => ['0.0.0.0', 4000], | |
# AnyEvent will run this callback when getting some input | |
on_recv => sub { | |
my ($data, $ae_handle, $client_addr) = @_; | |
chomp $data; | |
AE::log warn => "Received '$data' (handle: $ae_handle)"; | |
# Send back the command echoed to the client who contacted us | |
$ae_handle->push_send("echo [$data]", $client_addr); | |
} | |
); | |
# Start the main event loop | |
my $condvar = AE::cv; | |
$condvar->recv; | |
# Never gets here. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment