我在 iOS 上使用 Xamarin + MonoTouch 来浏览网络上的 Web 服务器,然后我可以从中下载文件。
传递给解析事件处理程序的 NSNetService 包含地址作为 NSData。我找不到将 NSData 转换为实际 IP 地址的好方法,然后我可以从中构建 URL,即 http:// < IPAddress > /folder/file.htm
这是我的 NSNetService.AddressResolved 事件处理程序:
private void OnServiceResolved(object sender, EventArgs args)
{
NSNetService service = (NSNetService)sender;
// service.Port is valid.
// service.HostName is valid.
// but we want the IP addres, which is in service.Addresses.
// None of the following four methods works quite right.
IPAddress address = (IPAddress)service.Addresses [0]; // Cannot convert type NSData to IPAddress
SocketAddress address2 = (SocketAddress)service.Addresses[0]; // Cannot convert NSData to SocketAddress. A binary copy might work?
IPHostEntry entry = (IPHostEntry)service.Addresses [0]; // Cannot convert NSData to IPHostEntry
IPHostEntry entry2 = Dns.GetHostByName (service.HostName); // This kinda works, but is dumb. Didn't we just resolve?
}
在解析事件中从 NSNetService 获取服务 IP 地址的正确方法是什么?