Search code examples
tcppingazure-iot-hubicmp

Availability check for IotHub Using Ping C# Does not work ICMP Echo packet in container as the outbound does not allow ICMP


I am trying to check availability of an IoTHub using Ping,

public async Task<bool> CheckAvailabilityAsync(string url)
{
    if (string.IsNullOrEmpty(url)) return false;
    Ping ping = new Ping();
    try
    {
        var result = await ping.SendPingAsync(url);
        return result.Status == IPStatus.Success;
    }
    catch (Exception ex)
    {
        return false;
    }
}

here url is "notavilableiothubName.azure-devices.net". look like ICMP is not supported for outbound ! What is the best way to achieve this ! Is there a native availability check for iothubs?


Solution

  • Alternatively I am planning to use socket connection test.

     try
     {
         using (var socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { Blocking = true })
         {
             var task = socket.ConnectAsync(url, 443);
             await task.WaitAsync(TimeSpan.FromSeconds(10));           
             return true;
         }
     }
     catch (Exception)
     {
         return false;
     }
    

    Supporting ports available documented.

    https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-protocols#port-numbers

    Not sure if this is the best way, suggestions welcome!