Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How best to support IPv4/v6 in Perl server

by VinsWorldcom (Parson)
on Jan 26, 2012 at 16:04 UTC ( #950147=perlquestion: print w/replies, xml ) Need Help??
VinsWorldcom has asked for the wisdom of the Perl Monks concerning the following question:

QUESTION:What's the best cross-platform / backward compatible way to support IPv4 and IPv6 in Perl servers?

My research / testing:

I have a few modules on CPAN that create simple servers for listening and parsing of messages (shameless plug: Net::Syslogd and Net::SNMPTrapd). Currently, they support IPv4 by creating the listener with IO::Socket::INET.

I've been doing some work / testing and some reading on Perl support for IPv6 lately (IPv6 Name Resolution). I'm wondering what the best way to support both IPv4 and IPv6 in a Perl server would be?

Currently, I've done something with IO::Select and both IO::Socket::INET/6. Psuedo code below:

... # determine v4 or v6, nothing selected - then both if (!defined($opt{ipv4}) && !defined($opt{ipv6})) { $opt{ipv4} = $opt{ipv6} = 1 } ... # server (select between v4 and v6 if both exist) my $server = IO::Select->new(); # v4 server if ($opt{ipv4}) { my $server4 = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $opt{port}, Listen => SOMAXCONN, Reuse => 1 ) or die "$0: Unable to create + IPv4 server: $!\n"; $server->add($server4) } # v6 server if ($opt{ipv6}) { my $server6 = IO::Socket::INET6->new( Proto => 'tcp', LocalPort => $opt{port}, Listen => SOMAXCONN, Reuse => 1 ) or die "$0: Unable to creat +e IPv6 server: $!\n"; $server->add($server6) } ... # determine which server has data while (my @servers = $server->can_read) { # loop servers for my $svr (@servers) { # read from v4 or v6 server that has data while(my $client = $svr->accept()) { #DO STUFF } } }

The code works fine - no problem / debug there. According to 'corelist', Socket6, IO::Socket::INET6 and "the new drop-in replacement" IO::Socket::IP aren't in 5.12.3 (the version I'm using) and don't appear to be in 5.14 according to

So what are your opinions on the best way to support IPv4 and IPv6 in Perl servers:

  • IO::Select with IO::Socket::INET / INET6
  • IO::Socket::IP
  • Socket and Socket6 with some fierce coding
  • Something else?

Replies are listed 'Best First'.
Re: How best to support IPv4/v6 in Perl server
by Anonymous Monk on Jan 26, 2012 at 17:18 UTC
    That's easy. If you have Perl 5.14 or later, use IO::Socket::INET (core module, IP-dualstack). Any other Perl, install and use IO::Socket::IP (CPAN module, IP-dualstack).

      Didn't realize that - so in 5.14 and on, IO::Socket::INET *includes* IPv6?

      Of course, as you point out, that won't be backward compatible, I'll still need to 'use' IO::Socket::IP or IO::Socket::INET6 for older Perl versions.


      Anyone else have an opinion?

Re: How best to support IPv4/v6 in Perl server
by Rhandom (Curate) on Jan 30, 2012 at 15:11 UTC
    Sorry. I'm not done quite yet. I've had changes pending in Net::Server for nearly a year adding IPv6 support (thanks to patches from half a dozen people). Once I'm done then you can just start up Net::Server and you get IPv4 and 6 for free.
    my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950147]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2017-04-30 07:24 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (534 votes). Check out past polls.