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

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

问题描述

我正在将 WP7 应用程序移植到 Windows 8 Metro,我遇到的(许多)转换障碍之一是根据主机名或 DNS 名称发现 IP 地址.以下是我之前在 WP7 中使用的示例:

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);

我已经在网上搜索了解决方案并浏览了 Metro API,但我还没有找到任何东西.有没有其他人在使用 Metro/WinRT 时遇到过这个问题并找到了解决方案?

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

一旦 clientSocket 尝试连接,clientSocket.Information 属性将与大量网络信息混合,包括远程主机信息,包括 ip 地址.我只是内联输入了这个,所以我希望没有错误.希望这可以帮助!也可以试试 这个指向 msdn 的链接.

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天全站免登陆