How to resolve a hostname to an IP address in Metro/WinRT?

前端 未结 1 682
无人共我
无人共我 2021-01-15 05:54

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

相关标签:
1条回答
  • 2021-01-15 06:34
    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.

    0 讨论(0)
提交回复
热议问题