Question

Given a pair of hostname and port (i.e. www.stackoverflow.com, port 80) I want to get all the IP associated with them. Also, I want to list them into a listview within Windows Phone screen. I've seen ResolveHostNameAsync class, and even implemented an example of it, but I still not get how to properly use it, as in my example the callback method is never called.

My code is as bellow:

   // Constructor
   public StartPage() {
      InitializeComponent();
      ResolveHostName("www.stackoverflow.com");
   }
   private void ResolveHostName(String hostName) {
      var endPoint = new DnsEndPoint(hostName, 80);
      DeviceNetworkInformation.ResolveHostNameAsync(
         endPoint,
         OnNameResolved,
         null
      );
   }

   private void OnNameResolved(NameResolutionResult result) {
      IPEndPoint[] endpoints = result.IPEndPoints;
      // Here your address if resolved
      if (endpoints != null && endpoints.Length > 0) {
         var ipAddress = endpoints[0].Address;
         testData.Text += ipAddress;
      } else {
         testData.Text += "empty";
      }
   }

testData is a TextBlock just to show the first IP. Funny thing is that even "empty" is shown, so I wonder if the callback OnNameResolved is really called.

Does someone can lend a help on how to proceed? Thanks in advance.

Was it helpful?

Solution

I just got how to solve my problem. I've used a diferent approach, with async method and Sockets. I've called this bellow method in the StartPage constructor and the testData has showed the desired value.

private async void getIPs() { HostName serverHost = new HostName("www.stackoverflow.com"); var clientSocket = new Windows.Networking.Sockets.StreamSocket(); // Try to connect to the remote host await clientSocket.ConnectAsync(serverHost, "http"); // Get the HostName as DisplayName, CanonicalName, Raw with the IpAddress. var ipAddress = clientSocket.Information.RemoteAddress.DisplayName; testData.Text += ipAddress; }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top