简体   繁体   English

WP7芒果 - 如何获取给定主机名的IP地址

[英]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. 我需要从DnsEndPoint获取给定主机名的IP地址,并将其转换为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? WP7缺少Dns.GetHostEntry函数,所以有没有办法在不创建Socket的情况下执行此操作,将数据发送到主机,然后从主机接收ping并读取RemoteEndPoint属性以获取主机的IP地址?

Try using DeviceNetworkInformation.ResolveHostNameAsync in the Microsoft.Phone.Net.NetworkInformation namespace, like this: 尝试在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
}

There is no way to do this built into the framework. 没有办法在框架中构建这个。 You could use a socket assumming that the host supports ping. 您可以使用套接字来确定主机支持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. 让您的应用程序使用IP地址可能更容易,如果您拥有的只是IP地址,则不需要主机名。

I think Im dealing with the same problem. 我想我处理同样的问题。 I also have a dynamic IP updating the dns with No-ip. 我还有一个动态IP用No-ip更新dns。

For what I know the System.Net.Dns is not available in this version of Windows Phone. 据我所知,此版本的Windows Phone中没有System.Net.Dns。 Maybe in next releases. 也许在下一个版本中。

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

At the start of my app Im going to create a web service call to the host (to the webserver in it) asking for the IPAddress. 在我的应用程序开始时,我将创建一个Web服务调用主机(到其中的Web服务器),要求IPAddress。 I think I'll solve the problem in the meantime. 我想我会在此期间解决问题。

This could be the WCF service 这可能是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();
    }
  }

If you guys find a direct approach please let me know 如果你们找到直接的方法,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM