Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the IPAddress from HostName in c# windows 8 Metro App? [duplicate]

Tags:

c#

windows-8

I'm Migrating Windows Form Class Library to Metro App Class Library. In that there's a code-block which gives the IPAddress from the Host Name, below,

IPHostEntry ipHostInfo = Dns.GetHostEntry(Address);   
IPAddress ipAddress = ipHostInfo.AddressList[0];// IPAddress.Parse(address);
IPEndPoint endPoint = new IPEndPoint(ipAddress, Port);

eg:

Address : talk.google.com

ipAddress : xx.xxx.xxx.xx

But I've seen that there is no IPHostEntry or Dns or IPAddress in Metro App System.Net. .

If anybody knows means please tell me the replacement for these in windows 8 metro app.

like image 223
Gopinath Perumal Avatar asked Nov 21 '12 09:11

Gopinath Perumal


People also ask

How can I get IP address from hostname?

In an open command line, type ping followed by the hostname (for example, ping dotcom-monitor.com). and press Enter. The command line will show the IP address of the requested web resource in the response. An alternative way to call Command Prompt is the keyboard shortcut Win + R.

How do I find my host C IP address?

Here is a simple method to find hostname and IP address using C program. gethostname() : The gethostname function retrieves the standard host name for the local computer. gethostbyname() : The gethostbyname function retrieves host information corresponding to a host name from a host database.

Can we get IP address from domain name?

Typically you can find the IP address for a domain by pinging the domain. This can be done from your local PC. However, the steps may vary depending on the OS that you're using. To simplify this process, we can use an online Ping tool, such as https://ping.eu/ping/.


1 Answers

using System.Threading.Tasks;

public async static Task<string> ResolveDNS(string remoteHostName)
    {
        if (string.IsNullOrEmpty(remoteHostName))
            return string.Empty;

        string ipAddress = string.Empty;

        try
        {
            IReadOnlyList<EndpointPair> data =
              await DatagramSocket.GetEndpointPairsAsync(new HostName(remoteHostName), "0");

            if (data != null && data.Count > 0)
            {
                foreach (EndpointPair item in data)
                {
                    if (item != null && item.RemoteHostName != null &&
                                  item.RemoteHostName.Type == HostNameType.Ipv4)
                    {
                        return item.RemoteHostName.CanonicalName;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ipAddress = ex.Message;
        }

        return ipAddress;
    } 
like image 183
depicus Avatar answered Sep 27 '22 23:09

depicus