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?