Safabyte NetXtremeDns v1.2 Send comments on this topic to Safabyte.
Asynchronously query DNS information

Glossary Item Box

The example below shows how to asynchronously send a DNS request message to a DNS server and display the received response:

C# Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using Safabyte.Net.Dns;
namespace CSharp
{
   
static class Async
   {
       
static System.Threading.ManualResetEvent _mn;
       [STAThread]
       
static void Main()
       {
           _mn =
new System.Threading.ManualResetEvent(false);
           DnsClient client =
new DnsClient();
           client.AsyncExceptionRaised +=
new EventHandler<AsyncExceptionRaisedEventArgs>(client_AsyncExceptionRaised);
           client.LookupCompleted +=
new EventHandler<LookupCompletedEventArgs>(client_LookupCompleted);
           client.BeginLookup(
"yahoo.com", QueryType.ALL);
           _mn.WaitOne();
       }
       
static void client_LookupCompleted(object sender, LookupCompletedEventArgs e)
       {
           DnsClient client = sender
as DnsClient;
           
bool result = client.EndLookup(e.AsyncResult);
           
if (client.ReplyMessage != null)
           {
               
if (client.ReplyMessage.Answers.Count > 0)
               {
                   Console.WriteLine(
"Answer Records:");
                   
foreach (ResourceRecord r in client.ReplyMessage.Answers)
                   {
                       Console.WriteLine(
"  " + r.ToString());
                   }
               }
               
if (client.ReplyMessage.Authorities.Count > 0)
               {
                   Console.WriteLine(
"");
                   Console.WriteLine(
"Authority Records:");
                   
foreach (ResourceRecord r in client.ReplyMessage.Authorities)
                   {
                       Console.WriteLine(
"  " + r.ToString());
                   }
               }
               
if (client.ReplyMessage.AdditionalRecords.Count > 0)
               {
                   Console.WriteLine(
"");
                   Console.WriteLine(
"Additional Records:");
                   
foreach (ResourceRecord r in client.ReplyMessage.AdditionalRecords)
                   {
                       Console.WriteLine(
"  " + r.ToString());
                   }
               }
           }
           _mn.Set();
       }
       
static void client_AsyncExceptionRaised(object sender, AsyncExceptionRaisedEventArgs e)
       {
           Console.WriteLine(
"An error occurred: " + e.Exception.Message);
       }        
   }
}
VB.NET Copy Code
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Safabyte.Net.Dns
Module Async
    Dim _mn As System.Threading.ManualResetEvent
    Sub Main()
        _mn = New System.Threading.ManualResetEvent(False)
        Dim client As New DnsClient()
        AddHandler client.AsyncExceptionRaised, AddressOf client_AsyncExceptionRaised
        AddHandler client.LookupCompleted, AddressOf client_LookupCompleted
        client.BeginLookup("yahoo.com", QueryType.ALL)
        _mn.WaitOne()
    End Sub
    Private Sub client_LookupCompleted(ByVal sender As Object, ByVal e As LookupCompletedEventArgs)
        Dim client As DnsClient = TryCast(sender, DnsClient)
        Dim result As Boolean = client.EndLookup(e.AsyncResult)
        If client.ReplyMessage IsNot Nothing Then
            If client.ReplyMessage.Answers.Count > 0 Then
                Console.WriteLine("Answer Records:")
                For Each r As ResourceRecord In client.ReplyMessage.Answers
                    Console.WriteLine(" " + r.ToString())
                Next
            End If
            If client.ReplyMessage.Authorities.Count > 0 Then
                Console.WriteLine("")
                Console.WriteLine("Authority Records:")
                For Each r As ResourceRecord In client.ReplyMessage.Authorities
                    Console.WriteLine(" " + r.ToString())
                Next
            End If
            If client.ReplyMessage.AdditionalRecords.Count > 0 Then
                Console.WriteLine("")
                Console.WriteLine("Additional Records:")
                For Each r As ResourceRecord In client.ReplyMessage.AdditionalRecords
                    Console.WriteLine(" " + r.ToString())
                Next
            End If
        End If
        _mn.[Set]()
    End Sub
    Private Sub client_AsyncExceptionRaised(ByVal sender As Object, ByVal e As AsyncExceptionRaisedEventArgs)
        Console.WriteLine("An error occurred: " + e.Exception.Message)
    End Sub
End Module