Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Ping() giving false timeouts?

I have a script that is looping through and sending a ping to a host every second. The purpose of which is to test wireless performance on my home network. The issue is, I seem to be getting timeouts from my script, while a standard "ping -t" on either client (pingER and pingEE) have no such timeouts. In fact, not once in the last 30 minutes has either timed out, while my script has received timeout replies every few minutes.

My essential script is below. In all cases, when it does happen, my reply is TimedOut with a response time of 0ms. Is my script causing false timeouts? Thanks.

EDIT: updated code to reflect newest changes, still having same behavior.

while (true)
{
    System.Threading.Thread.Sleep(delay);
    var builder = new StringBuilder();

    hosts.ForEach(h =>
    {
        var buffer = new byte[32];
        var reply = ping.Send(h, 4000, buffer, new PingOptions(600, true));
        var error = reply.Status != IPStatus.Success || reply.RoundtripTime > maxRoundTrip;

        if (verbose || error)
        {
            builder.AppendFormat("{0}: {1} ({2}ms)", h, reply.Status, reply.RoundtripTime);
            builder.Append(Environment.NewLine);

            if (error)
            {
                WriteLog(h, reply);
                SendEmail(h, reply);
            }
        }
    });

    Console.WriteLine(DateTime.Now);
    Console.WriteLine(builder.ToString());
}
like image 645
Ryan Peters Avatar asked Mar 29 '14 01:03

Ryan Peters


2 Answers

If your ping -t timeout value is less than 1000 or the value of maxRoundTrip is less than 1000 then you are checking against different timeouts in your two different pings (.NET Ping.Send vs. ping -t). The default Windows ping timeout is 4000 ms. http://technet.microsoft.com/en-us/library/bb490968.aspx

Also in your .NET code, you are sending the max buffer size of 65500 bytes. The default Windows ping command sends only 32 bytes of data.

So the difference between either the timeout or buffer size is likely what is causing the variance.

More info about the .NET Ping.Send method: http://msdn.microsoft.com/en-us/library/ms144956(v=vs.110).aspx

like image 181
BateTech Avatar answered Oct 08 '22 15:10

BateTech


I might have figured it out. I was using the netbios name for the machine in question. I'm guessing that each time it did a ping, it was factoring in the IP lookup in the round trip and was timing out. When I used just the IP, it worked just fine and hasn't timed out.

Thanks for the input.

like image 21
Ryan Peters Avatar answered Oct 08 '22 14:10

Ryan Peters