mpapet has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Borrowing some code found on perlmonks.org, I am attempting a udp relay server as a programming exercise. I'm using the strawberry perl distro. It's built with mingw and has great cpan support

I learned that IO::Socket just doesn't work on Win32, so I used Socket. What I need to do next is to at least examine the win32 UDP packets in an attempt to rewrite the destination to another subnet. NetPacket::UDP doesn't give me anything to work with. I don't know where, or how many places I've gone wrong, so I'm ready for a few lessons. Here's the code I used:

use warnings; use strict; use NetPacket::UDP; use NetPacket::Ethernet qw(:strip); use Socket qw(:all); use Data::Dumper $|++; # no suffering from buffering my $udp_port = 1434; socket( UDPSOCK, PF_INET, SOCK_DGRAM, getprotobyname('udp') ) or die " +socket: $!"; select( ( select(UDPSOCK), $|=1 )[0] ); # no suffering from buffering setsockopt( UDPSOCK, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "setsockopt SO_REUSEADDR: $!"; setsockopt( UDPSOCK, SOL_SOCKET, SO_BROADCAST, 1 ) or die "setsockopt SO_BROADCAST: $!"; my $broadcastAddr = sockaddr_in( $udp_port, INADDR_ANY ); bind( UDPSOCK, $broadcastAddr ) or die "bind failed: $!\n"; my $input; while( my $addr = recv( UDPSOCK, $input, 1024, 0 ) ) { print "$addr => $input\n"; my $ip_obj = NetPacket::UDP::decode(NetPacket::Ethernet::eth_stri +p($addr)); my $udp_obj = NetPacket::UDP->decode($ip_obj->{data}); # my $udp_obj = NetPacket::UDP->decode($ip_obj->{data}); my $test = $ip_obj->{dest_ip}; print " $test \n"; }
Output::

☻ ♂~¼▼Å0 => ☻

Use of uninitialized value $test in concatenation (.) or string at socket2.pl line 33.

Replies are listed 'Best First'.
Re: UDP Server on Win32
by ikegami (Patriarch) on Mar 09, 2009 at 22:33 UTC

    I learned that IO::Socket just doesn't work on Win32, so I used Socket.

    It works perfectly fine on Windows. It's a Pure Perl wrapper for the functions you ended up using anyway.

Re: UDP Server on Win32
by NetWallah (Canon) on Mar 10, 2009 at 04:41 UTC
    Recycling some code I wrote in 2004 for just this purpose. Implemented on Windows (Tested with Active perl). Hope it helps. And yes, IO::Socket works fine on Win32.

         ..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.

      Under the following conditions,

      1. Win 2003 server

      2. Strawberry Perl (Not activestate's distro)

      3. I do not use the listen parameter.

      then IO::Socket::INET "works."

      If I use the listen parameter, then the code errors out with an "unknown error" message. The code provided in the comments works because the listen parameter is not used.

      Thank you for the code. It works great.