Pen Truth

Mike Trethowan

One of the products that I occasionally work with is the Xport Direct from Lantronix.  One of the shortcomings is that the DNS server on LAN routers will not return the DHCP Device Name when a DNS request is made.  This is not very convenient when you require your customer to set aside an IP before the installation.  This means that you can’t simply create a Client Socket and connect using only the Host Name and Port Number.  This also means that for one to write software to communicate with the device you have to give the Xport Direct a fixed IP address and connect using the IP and Port Number.

 

While the problem I am trying to solve still exists, I created this project to find out what information was accessible from my devise.  This Demo Project will ping all the IPs on your network and list them and some stats in a ListBox.  There is also a Select Case statement in my code to give the Reply.Status of the ping.  My search for these codes were unfruitful so I did a small routine to print them out for me, so now you have them too.  If I find a satisfactory and easy resolution to the DNS problem, I will post that  too.

This demo also contains a Delegate for CrossThreadding access to the ListBox and a BackgroundWorker, so if you don’t have experience with these you can see how they operate.

Lets Begin:
Create a basic VB.Net Windows Form Project.  Add a ListBox to your form and in the properties set Dock to Fill.  This way, as you drag the form to resize it the ListBox will resize also.  Next add a BackgroundWorker from the tool box. You will find this under Components.  Save your project and then go into the Code View.  If you have not renamed anything you should be able to simply copy the following code and drop it onto your code page.  Tip: you can delete everything on the code page first before dropping my code onto it.  Have fun.

Imports System.ComponentModel
Imports System.Net.NetworkInformation
Imports System.Net
Imports System.Text

Public Class Form1

    Dim ip As String = "192.168.1." ' Basic default for most routers is 192.168.1.1 --- Leave off the last number here

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BackgroundWorker1.RunWorkerAsync()
    End Sub

#Region " Background worker "

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
            Dim pingSender As New Ping()
            Dim buffer() As Byte = Encoding.ASCII.GetBytes("WeJustWantToSendSomething")
            Dim timeout As Integer = 1000
            Dim options As New PingOptions(64, True)
            Dim reply As PingReply

            displayMessage(ListBox1, "Search Started")

            ' Iterate through all the possible addresses
            For i = 2 To 254 ' Start at 2 because 1 will be the Router
                reply = pingSender.Send(ip & i.ToString, timeout, buffer, options)
                If reply.Status = IPStatus.Success Then
                    Try
                        displayMessage(ListBox1, "Host Name: " & System.Net.Dns.GetHostEntry(ip & i.ToString).HostName)
                    Catch ex As Exception
                        displayMessage(ListBox1, "Host Name: Not Reporting")
                    End Try
                    displayMessage(ListBox1, "Address: " & reply.Address.ToString())
                    displayMessage(ListBox1, "Time to live: " & reply.Options.Ttl)
                    displayMessage(ListBox1, "Don't fragment: " & reply.Options.DontFragment)

                    Dim s As String
                    Select Case reply.Status
                        Case -1 : s = "Unknown"
                        Case 0 : s = "Success"
                        Case 11002 : s = "Destination Network Unreachable"
                        Case 11003 : s = "Destination Host Unreachable"
                        Case 11004 : s = "Destination Prohibited / Destination Protocol Unreachable"
                        Case 11005 : s = "Destination PortUnreachable"
                        Case 11006 : s = "No Resources"
                        Case 11007 : s = "Bad Option"
                        Case 11008 : s = "Hardware Error"
                        Case 11009 : s = "Packet Too Big"
                        Case 11010 : s = "Timed Out"
                        Case 11012 : s = "Bad Route"
                        Case 11013 : s = "Ttl Expired"
                        Case 11014 : s = "Ttl Reassembly Time Exceeded"
                        Case 11015 : s = "Parameter Problem"
                        Case 11016 : s = "Source Quench"
                        Case 11018 : s = "Bad Destination"
                        Case 11040 : s = "Destination Unreachable"
                        Case 11041 : s = "Time Exceeded"
                        Case 11042 : s = "Bad Header"
                        Case 11043 : s = "Unrecognized Next Header"
                        Case 11044 : s = "Icmp Error"
                        Case 11045 : s = "Destination Scope Mismatch"
                        Case Else : s = reply.Status
                    End Select
                    displayMessage(ListBox1, "Device Status: " & s)

                    displayMessage(ListBox1, "Round trip Time: " & reply.RoundtripTime.ToString & "ms")
                    displayMessage(ListBox1, "")
                End If
            Next
        Catch ex As Exception
            displayMessage(ListBox1, ex.Message)
        End Try
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Try
            displayMessage(ListBox1, "=============== DONE ===============")
        Catch ex As Exception
            ListBox1.Items.Add("Error: bgwGetMessage " & ex.Message)
        End Try
    End Sub

#End Region

#Region " Delegates "

    Private Delegate Sub MessageDelagate(ByVal lbx As ListBox, ByVal txt As String)

    Private Sub displayMessage(ByVal lbx As ListBox, ByVal txt As String)
        Try
            If lbx.InvokeRequired Then
                lbx.Invoke(New MessageDelagate(AddressOf displayMessage), New Object() {lbx, txt})
            Else
                lbx.Items.Add(txt)
            End If
        Catch ex As Exception
            ListBox1.Items.Add("Error: displayMessage() " & ex.Message)
        End Try
    End Sub

#End Region

End Class

Disclaimer:  The code in this tutorial is not intended as a final product or by no means the only way this project can be coded. The presented code is for educational purposes only and no warranty is granted or implied. I/we are not responsible for any damages as a result of using this code. Use at your own risk.

Comments are closed.