in reply to Activeperl 5.16.1 Ipv6 support
I'd suggest you be a bit more explicit about the type of socket you want to create. It's possible the version of Perl you're using can't figure out some of the details that you're expecting to be assumed properly. Of course, it's also possible the version of Perl or some library was built without IPv6 support, in which case there's not much you can do to fix it besides recompile.
For starters, you should always define the Proto and Type of socket you create. In this case it appears you want a TCP socket of a STREAM type (TCP sockets are always streams; other types of sockets, like Unix Domain, can be one of multiple types.)
You can also enforce a desired address Family, such as AF_INET for IPv4 or AF_INET6 for IPv6. These constants are defined in the Socket Perl class (and ultimately defined in the C library sys/socket.h where Perl gets them.) If you do not declare an address family, the kernel will determine an appropriate type through a gethostbyname(3) system call. For dual-stacked hosts, this may not be what you intend when using a DNS name, like google.com instead of a raw IP. We don't usually hard-code IPs in code for IPv6, and it's even bad practice in IPv4 in most cases.
Also, you probably want to use the $! variable in errors describing system calls, such as socket creation. The $@ variable holds the text of the last eval failure (this is how you "catch" exceptions in Perl) while the $! variable holds the C errno value, which will be translated to its human-readable error text when used in a string. Update: $@ may contain higher-level details on the failure, so sometimes "Socket error: $@ ($!)" can be helpful. See perlvar docs for details.
I've given you a bit more of a complete example below that creates a TCP socket in STREAM mode to an IPv6 endpoint for google on port 80. Then it'll send a request for the homepage and print results to STDOUT. This should work anywhere provided you have a sufficiently new-enough Perl (5.20 or better) that includes IO::Socket::IP, or have obtained that module from CPAN or similar.
use strict; use warnings; require IO::Socket::IP; # Implicitly included above, yet list it explicitly: require Socket; # An IPv6 IP would work fine here too. # Feel free to hard-code the CDN IPv6 resolution if you'd like. my $host = "google.com"; my $sock = IO::Socket::IP->new( Proto => Socket->IPPROTO_TCP, Type => Socket->SOCK_STREAM, # This will force a particular address-family. # If you leave it off, the system's gethostbyname() call # will attempt to determine this automatically: Family => Socket->AF_INET6, PeerHost => $host, PeerPort => 80, Timeout => 3, ) or die "Failed to create socket: $!"; # Use Perl/IO (built on Standard I/O) to send a request for data: $sock->print("GET /\n") or die "Socket error on write: $!"; # Read loop, printing out data from socket to STDOUT. my $rc; while (1) { # Attempt to read up to 1k of data: $rc = $sock->read(my $buffer, 1024); # catch socket errors (excluding EOF): unless (defined $rc) { die "Socket error on read: $!"; } # EOF returns 0, which exits the read loop: last if ($rc == 0); # Otherwise print the line, which may be a partial line. printf "%s", $buffer; } print "\n";
Note that using a socket like this is a fairly bad way to get a webpage, but this is simply a demonstration of how you normally use a client-socket.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Activeperl 5.16.1 Ipv6 support
by rama133101 (Novice) on Dec 01, 2015 at 06:45 UTC | |
by Apero (Scribe) on Dec 01, 2015 at 13:42 UTC | |
by rama133101 (Novice) on Jan 04, 2016 at 09:25 UTC | |
by Corion (Patriarch) on Jan 04, 2016 at 09:34 UTC | |
by Mayank09 (Initiate) on Jan 19, 2016 at 11:40 UTC | |
|