in reply to Re: gethostbyname returns reverse order of ipaddress expected
in thread gethostbyname returns reverse order of ipaddress expected

I should have been clearer its the choice of ipaddress to return that's the issue. If I return all addresses it get it in a reverse order than a C# program (both are copied below). if I use Khen1950fx's first example I get the answer as 192.168.0.1 which is the wrong address to use. Code that picks the first address to use now breaks down on a multiadapter machines.

Outputs via C# and perl

>listaddrcsharp.exe 10.123.67.139 192.168.0.1

>perl d:\temp\in.pl 192.168.0.1 10.123.67.139

Perl code
use Socket; use IO::Socket; my $machineName = lc($ENV{COMPUTERNAME}); my ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($mach +ineName); foreach(@addrs) { ($a,$b,$c,$d)=unpack ('C4', $_); print "$a.$b.$c.$d\n"; }
C# Code
public static void Main(string[] args) { IPAddress[] ips; ips = Dns.GetHostAddresses(Environment.MachineName); foreach (IPAddress ip in ips) { if (ip.AddressFamily == AddressFamily.InterNetwork) Console.WriteLine(" {0}", ip); } }

Replies are listed 'Best First'.
Re^3: gethostbyname returns reverse order of ipaddress expected
by Anonymous Monk on Jun 18, 2013 at 04:13 UTC
      As I described in the 2 outputs, the C# one returns addresses in the correct order (increasing interface metric) while the perl one returns it in the reverse order (decreasing metric). It has nothing to do with the byte order of the individual ip address.

        all perl does is call the system gethostbyname and returns the results, it doesn't reorder them; if the results don't match whatever C# does, if it doesn't match your expectations, doesn't mean the results are wrong