Glossary Item Box

PowerTCP SSL Sockets for .NET

Resolving Asynchronously

Asynchronous operations are useful because they occur without waiting for the action to complete before executing the next line of code. Such a technique is useful when an operation is time consuming and other code needs to execute without waiting for the operation to complete. The following example demonstrates resolving a host name to a dot address asynchronously using the BeginGetHost method. Asynchronous use works similarly for resolving email addresses (BeginGetMailHosts).

 

To resolve a host name to a dot address asynchronously.

  1. Add a button to the form.
  2. In this example, the BeginGetHost method will be used. The EndGetHost event will be raised when this operation has completed. "Wire-up" the EndGetHost event.
  3. Set the DNS server and begin to asynchronously resolve the host name.

    [Visual Basic, C#]

    [C#]
    // Set the DNS server to use.
    dns1.Server = "MyDnsServer";
    
    // Begin to resolve the host name.
    dns1.BeginGetHost("www.dart.com", null);
    
    [Visual Basic]
    ' Set the DNS server to use.
    Dns1.Server = "MyDnsServer"
    
    ' Begin to resolve the host name.
    Dns1.BeginGetHost("www.dart.com", Nothing)
    
  4. When the EndGetHost event is raised, take some action. In this example, the data is simply displayed. Your entire EndGetHost event should look something like the following.

    [Visual Basic, C#]

    [C#
    ]private void dns1_EndGetHost(object sender, Dart.PowerTCP.SslSockets.DnsEventArgs e)
    {
       // Check for any exceptions that occurred
       if(e.Exception == null)
       {
          // Display host name
          Debug.WriteLine("Host name: " + e.Host.HostName);
    
          // Display addresses, if any
          foreach(System.Net.IPAddress add in e.Host.AddressList)
             Debug.WriteLine("Address: " + add.ToString());
    
          // Display aliases, if any
          foreach(string s in e.Host.Aliases)
             Debug.WriteLine("Aliases: " + s);
       }
    }
    
    [Visual Basic]
    Private Sub Dns1_EndGetHost(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.DnsEventArgs) Handles Dns1.EndGetHost
       ' Check for any exceptions that occurred
       If e.Exception Is Nothing Then
       
          ' Display host name
          Debug.WriteLine("Host name: " + e.Host.HostName)
    
          ' Display addresses, if any
          Dim add As System.Net.IPAddress
          For Each add In e.Host.AddressList
             Debug.WriteLine("Address: " + add.ToString())
          Next
    
          ' Display aliases, if any
          Dim s As String
          For Each s In e.Host.Aliases
             Debug.WriteLine("Aliases: " + s)
          Next
       End If
    End Sub
    
  5. Compile and run the application.

 

In This Section

Resolving IP Addresses, Host Names and Email Addresses
Demonstrates how to resolve IP Addresses, host names and email addresses using the Dns component.
Resolving Asynchronously
Demonstrates how to use the Dns component to asynchronously resolve addresses.
Simple Pinging
Demonstrates how to ping a host.
Pinging Asynchronously
Demonstrates how to ping a host asynchronously.
Performing A Simple Trace Route Operation
Demonstrates how to trace a route to a host.
Performing A Trace Route Asynchronously
Demonstrates how to trace a route to a host asynchronously.

 

 


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.