C# Dns.GetHostEntry() - exception: No such host is known

0

From documentation Dns.GetHostEntry() Resolves a host name or IP address to an IPHostEntry instance. Could anyone help me undestand why this function doesn't work for some external IP address (it works for internal IP address) and the ping command works normally?

My code is:

using System;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Please enter a host name or IP address");
                Console.WriteLine("Usage: ConsoleApplication1 8.8.8.8");
                return 1;
            }

            string addr = args[0];

            try
            {
                IPHostEntry host = Dns.GetHostEntry(addr);
                Console.WriteLine($"GetHostEntry({addr}) returns HostName: {host.HostName}");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e);
            }

            return 0;
        }

    }
}

Output:

C:\Users\Administrator\Desktop>ping stackoverflow.com

Pinging stackoverflow.com [151.101.1.69] with 32 bytes of data:
Reply from 151.101.1.69: bytes=32 time=35ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57

Ping statistics for 151.101.1.69:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 16ms, Maximum = 35ms, Average = 20ms

C:\Users\Administrator\Desktop>ConsoleApplication1.exe 151.101.1.69
Exception: System.Net.Sockets.SocketException (0x80004005): No such host is known
   at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
   at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
   at ConsoleApplication1.Program.Main(String[] args)

C:\Users\Administrator\Desktop>ping 151.101.1.69

Pinging 151.101.1.69 with 32 bytes of data:
Reply from 151.101.1.69: bytes=32 time=27ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=16ms TTL=57
Reply from 151.101.1.69: bytes=32 time=17ms TTL=57

Ping statistics for 151.101.1.69:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 16ms, Maximum = 27ms, Average = 19ms

I found a solution changing the file

C:\Windows\System32\drivers\etc\hosts

Adding the line:

151.101.1.69 stackoverflow.com

It works after this change

C:\Users\Administrator\Desktop>ConsoleApplication1.exe 151.101.1.69
GetHostEntry(151.101.1.69) returns HostName: stackoverflow.com

In my opinion, changing hosts file is not a good solution to solve this issue. Does anyone know a better solution to solve this?

c#
windows
networking
dns
asked on Stack Overflow Jan 16, 2020 by hungrh

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0