如何解决主机名在Metro / WinRT的IP地址? [英] How to resolve a hostname to an IP address in Metro/WinRT?

查看:150
本文介绍了如何解决主机名在Metro / WinRT的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个移植WP7应用到Windows 8 Metro和我所遇到的基于主机名或DNS名称发现一个IP地址(多)转换障碍之一的过程。以下是我以前使用WP7中的一个例子:

  DnsEndPoint dnsEnd =新DnsEndPoint(www.google.com ,80,AddressFamily.InterNetwork); 
DeviceNetworkInformation.ResolveHostNameAsync(dnsEnd,IPLookupCallbackMethod,这一点);



我在网上搜索了一个解决方案,并浏览了地铁API,但我没有发现任何东西然而。有没有其他人遇到这个问题,地铁/ WinRT中,发现一个解决办法?


解决方案

 使用Windows.Networking;使用Windows.Networking.Sockets 
;

主机名SERVERHOST =新的主机名(www.google.com);
StreamSocket ClientSocket的=新Windows.Networking.Sockets.StreamSocket();

//尝试连接到远程主机的
等待clientSocket.ConnectAsync(SERVERHOST,HTTP);

//现在尝试clientSocket.Information财产
//例如clientSocket.Information.RemoteAddress
//获取IP地址



一旦ClientSocket的试图连接时,clientSocket.Information属性将凭借丰富的网络信息,包括远程主机的信息,包括IP地址进行水合。所以我希望没有错误,我刚才输入这行内。希望这可以帮助!也可以尝试此链接到MSDN


I'm in the process of porting a WP7 app to Windows 8 Metro and one of the (many) conversion obstacles I've encountered is discovering an IP address based on a hostname or DNS name. The following is an example of what I was previously using in WP7:

DnsEndPoint dnsEnd = new DnsEndPoint("www.google.com", 80, AddressFamily.InterNetwork);
DeviceNetworkInformation.ResolveHostNameAsync(dnsEnd, IPLookupCallbackMethod, this);

I've searched online for a solution and browsed the Metro API, but I haven't found anything yet. Has anyone else encountered this problem with Metro/WinRT and found a solution?

解决方案

using Windows.Networking;
using Windows.Networking.Sockets;

HostName serverHost = new HostName("www.google.com");
StreamSocket clientSocket = new Windows.Networking.Sockets.StreamSocket();

// Try to connect to the remote host
await clientSocket.ConnectAsync(serverHost, "http");

// Now try the clientSocket.Information property
// e.g. clientSocket.Information.RemoteAddress
// to get the ip address

Once the clientSocket has attempted a connection, the clientSocket.Information property will be hydrated with a wealth of networking information including the remote host information including ip address. I just typed this inline so I hope there are no errors. Hope this helps! Also try this link to msdn.

这篇关于如何解决主机名在Metro / WinRT的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆