#!/usr/bin/perl # Sample program for selecting filehandles. # Copyright 2004 by Rocco Caputo. Free software. # Same terms as Perl itself. Have fun! use warnings; use strict; use IO::Socket::INET; use POE; POE::Session->create( inline_states => { _start => \&handle_start, got_input => \&handle_input, }, ); POE::Kernel->run(); exit; sub handle_start { my $udp_svr = IO::Socket::INET->new( Proto => "udp", LocalPort => 8675, ) or die $!; $_[KERNEL]->select_read($udp_svr, "got_input"); print( "This is a UDP service.\n", "Use `nc -u -w 1 localhost 8675` with it.\n" ); } sub handle_input { my $socket = $_[ARG0]; my $msg = ""; my $remote = recv($socket, $msg, 4096, 0); unless (defined $remote) { warn "recv error: $!"; $_[KERNEL]->select_read($socket); return; } $msg =~ tr[A-Za-z][N-ZA-Mn-za-m]; send($socket, $msg, 0, $remote); }