Service Cannot resolve DNS

JimS

Member
Joined
Jan 24, 2008
Messages
9
Programming Experience
3-5
Hi guys,

I'm writing a simple application which pings a range of IP addresses from a database, and simply records the result back into a table.

I initially wrote it as a console app and everything worked fine. Now that I've converted the code to run as a service it throws a socket error:

VB.NET:
System.Net.NetworkInformation.PingException: An exception occurred 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 timeout, Byte[] buffer, PingOptions options)
   --- End of inner exception stack trace ---
   at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
In the above example the input to the ping function was an IP address.

I'm confused, because it looks like it's failing a DNS lookup, but I'm passing it an IP address, not a host name. Is this some security setting that's different because the app runs as a service? Its running as my network account which has admin rights.

Here is the sub which performs the ping...

VB.NET:
Private Sub ping(ByVal Address As String)
        Dim _ping As New System.Net.NetworkInformation.Ping()

        Dim _options As New PingOptions()
        Dim _reply As PingReply

        myCore.log("Trying: " & Address)

        Dim time As Long = 0
        Dim hits As Integer = 0
        AvgTime = -1
        PacketLoss = 100


        Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" '32 Bytes
        Dim buffer As Byte()
        buffer = System.Text.Encoding.ASCII.GetBytes(data)

        Try

            _reply = _ping.Send(Address, 500, buffer, _options)

            myCore.log(_reply.Status)
            myCore.log(_reply.Address.ToString)

            time += _reply.RoundtripTime
            hits += 1


            AvgTime = time / hits
            PacketLoss = ((5 - hits) / hits) * 100

        Catch ex As Exception
            myCore.log("Error: " & Environment.NewLine & ex.ToString)

        End Try
    End Sub

Any help would be appreciated as this is starting to get frustrating!

Thanks,

Jim.
 
Last edited:
I'm confused, because it looks like it's failing a DNS lookup, but I'm passing it an IP address, not a host name.
When you give it an IP string it uses DNS services to lookup a IP address object. You can get around this by using the Parse method of IPAddress class. Net.IPAddress.Parse("ip string") returns a IP address object without using DNS services.
But I don't think this is the problem..
Is this some security setting that's different because the app runs as a service? Its running as my network account which has admin rights.
I have tested Ping from a Windows Service (including DNS lookup) and didn't get any problems, did it on XP SP2 and tested all three service accounts plus a user account, no problems with any of them. I think you may have one of two problems, either a firewall blocking, or stricter security in newer OS (like Vista).
 
Thanks for the reply John. I'm using XP sp2, and the Windows firewall is disabled as I am behind a corporate firewall.

I can ping the host from a command line, and also from the same code if it runs as a console application....

I will try and manually parse the IP address as you mentioned and see if this makes any difference. I'm stumped!
 
This gets more interesting...

Using Net.IPAddress.Parse I get the following:

VB.NET:
System.FormatException: An invalid IP address was specified.
   at System.Net.IPAddress.InternalParse(String ipString, Boolean tryParse)
   at System.Net.IPAddress.Parse(String ipString)

But the string being parsed is definately "172.24.6.100", one of our machines...

Really confused now.

Jim.
 
Ok for some reason it seems to be a probem with the string, as when I hard code the string as "172.24.6.100" it works.
 
Back
Top