1

I want to create hostname to IP address and vice versa converter. However I got a problem in which 1 hostname return 2 IP addresses and when I reconvert the IP address to hostname, only one of them return the correct hostname.

Example for convert CEGN5CG7260FR7.xxxxx.xxx it return 192.168.X.XX and 10.132.XXX.XXX but when reconvert back to hostname, only 10.132.XXX.XXX return the correct hostname.

This code snip for convert hostname to IP address:-

// Convert hostname to IP address
            IPHostEntry host = Dns.GetHostEntry(hostList[i]);
            IPAddress[] ipaddr = host.AddressList;
            // Loop through the IP Address array and add the IP address to IP List
            foreach (IPAddress addr in ipaddr)
            {
                if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                     ipList.Add(addr.ToString());
                }
            }
            // Display items in IP Address textbox
            foreach (var ip in ipList)
            {
                ips += ip + Environment.NewLine;
            }
            txtIP.Text = ips;

This code snip for convert IP address to hostname:-

// Convert IP address to hostname
            IPHostEntry IP = Dns.GetHostEntry(IPAddress.Parse(ip));
            if (IP != null)
            {
                 hostList.Add(IP.HostName);
            }
            // Display items in Hostname textbox
            foreach (var hn in hostList)
            {
                hosts += hn + Environment.NewLine;
            }
            txtHost.Text = hosts;

What I can do to only get and display 10.132.XXX.XXX IP address and ignore the first one?

HNA
  • 107
  • 2
  • 14
  • `var ip = ipList.FirstOrDefault(v => v.StartsWith("10.132"))`? O:) – ProgrammingLlama Dec 28 '18 at 09:17
  • i'm afraid that will only take those with starting with `10.132.` there is also a list of ip which start with `172.21` and so on. – HNA Dec 28 '18 at 09:21
  • whats the output for the other HostName? Is it blank or some other value? – Nikunj Kakadiya Dec 28 '18 at 09:28
  • It's not quite clear why you want to ignore the other IP address. If you want only IP address from a specific subnet then answer from John is enough. Otherwise you could take a first one. – montonero Dec 28 '18 at 09:51
  • @NikunjKakadiya the output is no host known for the `192.168.X.XX` address. – HNA Dec 31 '18 at 01:13
  • @montonero I want to ignore the other since it returns no host known. I wish to only use IP address with the known host when convert it. The first one is the `192.168.X.XX` returns no host known, that's why I need to ignore it. – HNA Dec 31 '18 at 01:18
  • 1
    Then you'll need to check all IPs and find which one will be resolved back to host name. In my opinion ignoring other IPs is not a very good practice although I don't know why you need this. – montonero Dec 31 '18 at 19:09
  • @montonero thanks for your idea. I also think that it is not wise to ignore others IP. will try implement your idea. thank you. – HNA Jan 02 '19 at 05:48

1 Answers1

1

You can try this code to find correct IP address.

 public static string GetLocalIPAddress()
    {
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip.ToString();
            }
        }
        throw new Exception("No network adapters with an IPv4 address in the system!");
    }
Chetan Sanghani
  • 2,058
  • 2
  • 21
  • 36