WP7 Mango - 如何为给定的主机名获取IP地址

5

我需要从一个DnsEndPoint获取给定主机名的IP地址,并将其转换为IPEndPoint。 我该如何做?WP7缺少Dns.GetHostEntry函数,所以有没有方法可以不创建Socket,发送数据到主机,然后从主机接收ping并读取RemoteEndPoint属性来获取主机的IP地址?

3个回答

5
尝试使用 Microsoft.Phone.Net.NetworkInformation 命名空间中的 DeviceNetworkInformation.ResolveHostNameAsync 方法,如下所示:
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
}

2
我认为我正在处理相同的问题。我也有一个动态IP,使用No-ip更新DNS。
据我所知,System.Net.Dns在这个版本的Windows Phone中不可用。也许在以后的版本中会有。

http://msdn.microsoft.com/en-us/library/system.net.dns.aspx

在我的应用程序开始时,我将创建一个Web服务调用到主机(其中包含Web服务器),请求IP地址。我认为我会在此期间解决这个问题。
这可能是WCF服务。
[ServiceContract]
 public interface IService1
 { 
 [OperationContract]
 string GetIpAddress(string value);
 }

 public class Service1 : IService1
 {
  public string GetIpAddress()
    {
  // Add the proper error handling and collection matching of course
        IPAddress s = Dns.GetHostAddresses("www.mysite.com")[0];
        return s.ToString();
    }
  }

如果你们找到了直接的方法,请告诉我。

2

框架中没有内置此功能的方法。假设主机支持ping,您可以使用套接字。这将取决于您运行的网络(我认为您无法控制此内容)和应用程序的确切要求。

如果您只有IP地址,通过IP地址让您的应用程序工作并不需要主机名可能更容易。


问题在于,我正在处理动态分配的IP地址,这些地址经常更改,并通过No-IP更新它们,但我猜测,再次与微软合作时,最好的解决方案有时是没有解决方案。 - bbosak
我最终设置了一个UDP服务来进行主机IP查找。目前并不是一个真正的“解决方案”,但它可能是目前最好的解决方法。 - bbosak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接