Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 Mango - How to get an IP address for a given hostname

I need to get an IP address for a given hostname from a DnsEndPoint, and convert it to an IPEndPoint. How would I go about doing this? WP7 lacks a Dns.GetHostEntry function, so is there any way to do this without creating a Socket, sending data to the host, then receiving a ping from the host and reading the RemoteEndPoint property to get the IP address of the host?

like image 386
bbosak Avatar asked Jul 23 '11 13:07

bbosak


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 get an IP address?

On an Android/tabletGo to your Wifi network settings, then select the network you're connected to. You'll find your IP address along with the other network information.


2 Answers

Try using DeviceNetworkInformation.ResolveHostNameAsync in the Microsoft.Phone.Net.NetworkInformation namespace, like this:

public void DnsLookup(string hostname)
{
    var endpoint = new DnsEndPoint(hostname, 0);
    DeviceNetworkInformation.ResolveHostNameAsync(endpoint, OnNameResolved, null);  
}

private void OnNameResolved(NameResolutionResult result)
{
    IPEndPoint[] endpoints = result.IPEndPoints;
    // Do something with your endpoints
}
like image 156
Eltariel Avatar answered Sep 21 '22 01:09

Eltariel


There is no way to do this built into the framework. You could use a socket assumming that the host supports ping. It will depend on the network you are running in (I'd assume you can't control this) and the exact requirements of the application.

It may be easier to get your app to work with IP addresses and not require a hostname if all you have is an IP address.

like image 43
Matt Lacey Avatar answered Sep 21 '22 01:09

Matt Lacey