PowerTCP Sockets for .NET
Dns Class
Members  Example 




Supplements System.Net.Dns with the ability to retrieve MX records for any email address or mail server. Supports socket binding to a specified local IPEndPoint.
Object Model
Dns Class
Syntax
'Declaration
 
Public Class Dns 
   Inherits ComponentBase
'Usage
 
Dim instance As Dns
public class Dns : ComponentBase 
public __gc class Dns : public ComponentBase 
public ref class Dns : public ComponentBase 
Example
The following example demonstrates performing a DNS lookup of a provided hostname, and an MX record lookup of a provided email address.
private void button1_Click(object sender, EventArgs e)
{
    dns1.Start(doMxLookup, EmailToResolveTextbox.Text);
}

private void button2_Click(object sender, EventArgs e)
{
    dns1.Start(doHostLookup, HostToResolveTextbox.Text);
}

/// <summary>
/// Performs an MX record lookup of the provided email address.
/// </summary>
/// <param name="slave">Object used to perform DNS lookup</param>
/// <param name="state">Passed in from the 2nd parameter of Dns.Start(). Email address to lookup.</param>
private void doMxLookup(DnsSlave slave, object state)
{
    MxHostEntry[] hostEntries = slave.GetMxHostEntries(state.ToString());
    string results = "The following mail servers were returned for " + state.ToString() + ":" + Environment.NewLine;
    foreach (MxHostEntry hostEntry in hostEntries)
        results += hostEntry.HostName + " : Preference: " + hostEntry.Preference.ToString() + Environment.NewLine;
    //Marshal result to the UI for display
    dns1.Marshal(results, null);
}

/// <summary>
/// Performs a DNS lookup of the provided hostname.
/// </summary>
/// <param name="slave">Object used to perform DNS lookup</param>
/// <param name="state">Passed in from the 2nd parameter of Dns.Start(). Hostname to lookup.</param>
private void doHostLookup(DnsSlave slave, object state)
{
    System.Net.IPHostEntry hostEntry = slave.GetHostEntry(state.ToString());
    string results = "The following IP addresses were returned for " + state.ToString() + ":" + Environment.NewLine;
    foreach (System.Net.IPAddress address in hostEntry.AddressList)
        results += address.ToString() + Environment.NewLine;
    //Marshal result to the UI for display
    dns1.Marshal(results, null);
}

/// <summary>
/// Raised when Dns.Marshal(string, object) is called.
/// </summary>
private void dns1_UserState(object sender, UserStateEventArgs e)
{
    textBox1.AppendText(e.Message + Environment.NewLine);
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    dns1.Start(AddressOf doMxLookup, EmailToResolveTextbox.Text)
End Sub

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    dns1.Start(AddressOf doHostLookup, HostToResolveTextbox.Text)
End Sub

''' <summary>
''' Performs an MX record lookup of the provided email address.
''' </summary>
''' <param name="slave">Object used to perform DNS lookup</param>
''' <param name="state">Passed in from the 2nd parameter of Dns.Start(). Email address to lookup.</param>
Private Sub doMxLookup(ByVal slave As DnsSlave, ByVal state As Object)
    Dim hostEntries() As MxHostEntry = slave.GetMxHostEntries(state.ToString())
    Dim results As String = "The following mail servers were returned for " & state.ToString() & ":" & Environment.NewLine
    For Each hostEntry As MxHostEntry In hostEntries
        results &= hostEntry.HostName & " : Preference: " & hostEntry.Preference.ToString() & Environment.NewLine
    Next hostEntry
    'Marshal result to the UI for display
    dns1.Marshal(results, Nothing)
End Sub

''' <summary>
''' Performs a DNS lookup of the provided hostname.
''' </summary>
''' <param name="slave">Object used to perform DNS lookup</param>
''' <param name="state">Passed in from the 2nd parameter of Dns.Start(). Hostname to lookup.</param>
Private Sub doHostLookup(ByVal slave As DnsSlave, ByVal state As Object)
    Dim hostEntry As System.Net.IPHostEntry = slave.GetHostEntry(state.ToString())
    Dim results As String = "The following IP addresses were returned for " & state.ToString() & ":" & Environment.NewLine
    For Each address As System.Net.IPAddress In hostEntry.AddressList
        results &= address.ToString() & Environment.NewLine
    Next address
    'Marshal result to the UI for display
    dns1.Marshal(results, Nothing)
End Sub

''' <summary>
''' Raised when Dns.Marshal(string, object) is called.
''' </summary>
Private Sub dns1_UserState(ByVal sender As Object, ByVal e As UserStateEventArgs)
    textBox1.AppendText(e.Message & Environment.NewLine)
End Sub
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         Dart.Sockets.ComponentBase
            Dart.Sockets.Dns

See Also

Reference

Dns Members
Dart.Sockets Namespace


PowerTCP Sockets for .NET Documentation Version 4.5
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic