Hi There,

I need a server that's listening on UDP port 5000.
I have successfully gotten a TCP client to listen on port 5000:
[perl]
#!/usr/bin/perl -w
# serverfork.pl - a server that forks a child
# process to handle client connections
use strict;
use IO::Socket;
use Sys::Hostname;
use POSIX qw(:sys_wait_h);
sub REAP {
    1 until (-1 == waitpid(-1, WNOHANG));
    $SIG{CHLD} = \&REAP;
}
$SIG{CHLD} = \&REAP;
my $sock = new IO::Socket::INET(
                   LocalHost => 'localhost',
                   LocalPort => 5000,
                   Proto     => 'tcp',
                   Listen    => SOMAXCONN,
                   Reuse     => 1);
$sock or die "no socket :$!";
STDOUT->autoflush(1);
my($new_sock, $buf, $kid);
print "listening on port 5000: $sock\n";
while ($new_sock = $sock->accept()) {
  print "executing fork\n";
    # execute a fork, if this is
    # the parent, its work is done,
    # go straight to continue
    next if $kid = fork;
    die "fork: $!" unless defined $kid;
    # child now...
    # close the server - not needed
    close $sock;
    while (defined($buf = <$new_sock>)) {
   chop $buf;
   print "buf: $buf\n";
    }
    exit;
} continue {
    print "will close socket: $new_sock\n";
    # parent closes the client since
    # it is not needed
    close $new_sock;
}

[/perl]
Now how do I modify this same code to listen for udp packets instead
of tcp ones?
Thank you,
Ron


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to