0

I have following C# code for pinging an IP, and writing the result to a log.

private async ValueTask PingMachine() {
        var timesToPing = 4;
        var counter = 1;

        while (counter <= timesToPing) {
            var reply = await Pinger();
            foreach (var r in reply) {
               TagService.log_PLC.AppendLine($" Pinged {TagService.IP_PLC_List} {counter} times time:{r.RoundtripTime} status: {r.Status.ToString()}");
            }
        }
        counter++;
}

private async Task<IEnumerable<PingReply>> Pinger() {
    List<string> addresses = new List<string>();
    if (Check_IP_Correct(TagService.IP_PLC_List) == true) {
        addresses.Add(TagService.IP_PLC_List);
    }

    var tasks = addresses.Select(async ip => await new Ping().SendPingAsync(ip, 1000));
    var reply = await Task.WhenAll(tasks);
    return reply;
}

When I ping an IP that is online normally there is no problem, but when I ping a specific IP that is unreachable for testing, I get always following in the log. (3 times TimedOut, the fourth reply is always DestinationHostUnreachable

Pinged 10.92.XXX.XX 1 times time:0 status: TimedOut
Pinged 10.92.XXX.XX 2 times time:0 status: TimedOut
Pinged 10.92.XXX.XX 3 times time:0 status: TimedOut
Pinged 10.92.XXX.XX 4 times time:0 status: DestinationHostUnreachable

When I do the test in the same server with the same IP, but this time directly in the command line I always get a reply like Destination host unreachable.

Reply from 100.72.XXX.XX: Destination host unreachable.
Reply from 100.72.XXX.XX: Destination host unreachable.
Reply from 100.72.XXX.XX: Destination host unreachable.
Reply from 100.72.XXX.XX: Destination host unreachable.

I think the replies in the command lines should be correct. Why am I getting different replies with my code? What could be wrong?

karel
  • 5,489
  • 46
  • 45
  • 50
Mdarende
  • 469
  • 3
  • 13
  • 4
    You have a timeout of 1000 set in your Ping. The windows ping command must have a longer timeout. – John V Aug 25 '23 at 09:43
  • 1
    As John says, Windows ping has a longer timeout. It defaults to 4 seconds: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/ping – ProgrammingLlama Aug 25 '23 at 09:44
  • 1
    @JohnV John you are absolutely right, after increasing timeout to 4000, the replies are excactly the same like in command line. Thank you so much,this was a detail that I have overseen. – Mdarende Aug 25 '23 at 10:39

1 Answers1

0

As far as I know, there is no specification in TCP/IP saying what you must tell the user when a ping gets no response back before timeout.

So each client just tries to give an appropriate and useful message to the user.

Looks like the creators of the ping.exe command line tool chose to say "Reply from 100.72.XXX.XX: Destination host unreachable."

And the creators of whatever library you are using to ping from c# chose "Pinged 10.92.XXX.XX 3 times time:0 status: TimedOut"

Does it matter to you that they are different?

MGOwen
  • 6,562
  • 13
  • 58
  • 67
  • 1
    "Request timed out" and "destination host unreachable" are supposed to be different things: [ping response "Request timed out." vs "Destination Host unreachable"](https://stackoverflow.com/questions/22110622/ping-response-request-timed-out-vs-destination-host-unreachable). – Andrew Morton Aug 25 '23 at 10:03
  • Thanks for the answer but when I increased my timeout from 1000ms to 4000ms like John suggested, I have got the same ping reply like the command line. So i think this should be the solution – Mdarende Aug 25 '23 at 10:38