Dns Look up Example
We will illustrate the DNS and IP-related classes with an example that looks up DNS names. This screenshot shows the DnsLookup example in action:
The sample application simply invites the user to type in a DNS name using the main textbox. When .the user clicks the Resolve button, the sample uses the Dns. Resol ve () method to retrieve an IPHostEntry reference and display the hostname and IP addresses. Note how the hostname displayed may be different Cram the name typed in. This can occur if one DNS name.(www.microsoft.com) simply acts as a proxy for another DNS name (www.mlcrosoft.akadns.net).
The DnsLOolcup application is a standard C+ Windows application, with the controls added as shown in the screenshot, giving them the names txtBoxlnput, btnResolve, txtBoxHostName, and listBoxIPs respectively. Then we simply add the following method to the Form! class as the event handler for the buttonResolve click event.
Notice how in this code we are careful to trap any exceptions. An exception may occur if the user types . in an invalid DNS name, or if the network is down.
After retrieving the IPHostEntry instance, we use the AddressList property to obtain an array containing the IP addresses, which we then iterate through with a foreach loop. For each entry we display the IP address as an integer and as a string. using the IPAddress. To String () method.