need help with ping streamreader

colmite

New member
Joined
May 29, 2007
Messages
2
Programming Experience
Beginner
Hi there,

I am trying to put together a small application that will ping a huge list of servers (1800) that are written to a text file. I want to ping the server before trying to query the server for information. the Ping seems to work fine if the server is on-line or if server is off-line, but has a DNS record. If the server is off-line or does not exist the app fails. This is my code:


VB.NET:
Imports System
Imports System.IO

Module Module1
    Dim Server As String

    Sub Main()

        Try

            Using sr As StreamReader = New StreamReader("c:\TestFile.txt")

                Do
                    Server = sr.ReadLine()
                    If My.Computer.Network.Ping(Server, 1000) Then
                        Console.WriteLine(Server & " Is Reachable!")

                    Else
                        Console.WriteLine(Server & " Is Off-Line!")
                    End If

                Loop Until Server Is Nothing
                sr.Close()
            End Using
        Catch E As Exception
            Console.WriteLine("The file could not be read:")
            Console.WriteLine(E.Message)
        End Try
    End Sub

End Module
********here is the error if I turn off Error Handling.

helpme\bin\Debug\helpme.exe"
neusclab Is Off-Line!
test1 Is Off-Line!

Unhandled Exception: System.Net.NetworkInformation.PingException: An exception o
ccurred during a Ping request. ---> System.Net.Sockets.SocketException: No such
host is known
at System.Net.Dns.GetAddrInfo(String name)
at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)

at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 ti
meout, Byte[] buffer, PingOptions options)
--- End of inner exception stack trace ---
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer)
at Microsoft.VisualBasic.Devices.Network.Ping(String hostNameOrAddress, Int32
timeout)
at ConsoleApplication1.Module1.Main() in C:\My Documents\Visual Studio 2005\Projects\helpme\helpme\Module1.vb:line 16
any help to overcome this problem would be greatly appreciated.

thx
 
Last edited by a moderator:
Your last ReadLine returns Nothing which you put into the Ping call as a String (Nothing = String.Empty = "") and Ping throws SocketException because there is no nameless server. You could change the loop to this:
VB.NET:
Do Until sr.Peek = -1

Loop
 
Back
Top