Pergunta

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());
}
Foi útil?

Solução 2

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.

Outras dicas

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

In my case, the byte array size is the main reason (I programmed a similar ping application)

When

var buffer = new byte[65500];

2014/3/30 11:29:26 8.8.8.8: TimedOut (0ms)

changed to

var buffer = new byte[32];

2014/3/30 11:32:50 8.8.8.8: Success (8ms)

I deduced that the ping buffer if too high then the ICMP echo message might be fragmented become more than one packets then only send out,therefore it cannot completed the ping within the timeout period. (But seem like OP mentioned in answer it is hostname resolve problem, then may be my deduction is wrong)

my sample code

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int delay = 1000;
            Ping ping = new Ping();
            //OP using something else?
            System.Net.IPAddress h = IPAddress.Parse("8.8.8.8");

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

                    //change buffer size change the result...
                    //var buffer = new byte[65500];
                    var buffer = new byte[32];
                    var reply = ping.Send(h, 1000, buffer, new PingOptions(600, false));
                    var error = reply.Status != IPStatus.Success || reply.RoundtripTime > 3000;

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

                Console.WriteLine(DateTime.Now);
                Console.WriteLine(builder.ToString());
            }
        }
    }
}

var reply = ping.Send(h, 1000, buffer, new PingOptions(600, false));

1000 is timeout (ms) for the ping reply...

I'm seeing the same behavior as the OP. My ping.Send() is only sending a buffer of byte[30] and always uses the explicit IP so no DNS/NetBIOS involved. I've tried varying timeout values but consistently see regular timeout returns from Send() vs zero timeouts when using ping from the command-line.

public PingCheck(string address, int timeout = 250, int pingNbr = 1)
{
    var rtn = IPAddress.TryParse(address, out IPAddress addr);
    if (rtn)
    {
        IpAddress = addr;

        var dataSend = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        var pinger = new Ping();
        var pingerOptions = new PingOptions();
        pingerOptions.DontFragment = true;
        var buffer = Encoding.ASCII.GetBytes(dataSend);

        var statusList = new List<IPStatus>();
        var lossList = new List<bool>();
        var tripList = new List<long>();

        for (int i=0; i<pingNbr; i++)
        {
            var pingerReply = pinger.Send(addr, timeout, buffer, pingerOptions);
            var bufback = pingerReply.Buffer;
            var dataBack = Encoding.ASCII.GetString(bufback);

            var status = pingerReply.Status;
            var loss = (dataSend != dataBack);
            var msecs = pingerReply.RoundtripTime;

            statusList.Add(status);
            lossList.Add(loss);
            tripList.Add(msecs);
        }

       // TODO: Add statistics check
    }
    else
        throw new PingException("No IP Address");
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top