Search Unity

Question Problems with Ping on Android

Discussion in 'Multiplayer' started by kancsa, May 1, 2023.

  1. kancsa

    kancsa

    Joined:
    Feb 2, 2022
    Posts:
    9
    Hey,

    For my Android media app I'm trying to detect a WebDAV server that is being run locally - It's a media server that runs over HTTP basically.

    For this, I get the subnet (i.e 192.168.1.) then ping every IP in that subnet with multiple threads using Tasks.
    With IPs that respond I then filter again.

    In the editor this works:

    Code (CSharp):
    1.  
    2. System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
    3.  
    4.         for (int i = 2; i < 255; i++)
    5.         {  
    6.             string ip = $"{subnet}.{i}";
    7.             var task = PingSubnetAsync(ping, ip);
    8.         }
    9. ( ... )
    10. private async Task PingSubnetAsync(System.Net.NetworkInformation.Ping ping, string ip)
    11.     {
    12.         await Task.Run(() =>
    13.         {
    14.             PingReply reply = ping.Send(ip, 100);
    15.            
    16.             if (reply.Status == IPStatus.Success)
    17.             {
    18.                 try
    19.                 {
    20.                     IPHostEntry host = Dns.GetHostEntry(IPAddress.Parse(ip));
    21.                     UnityEngine.Debug.Log($"{ip}, {host.HostName}, Up\n");
    22.                     lock(lockObj){
    23.                         ipToHostname.Add(ip, host.HostName);
    24.                     }
    25.                    
    26.                 }
    27.                 catch
    28.                 {
    29.                     UnityEngine.Debug.LogError($"Couldn't retrieve hostname from {ip}");
    30.                     lock(lockObj){
    31.                         ipToHostname.Add(ip, "Unknown");
    32.                     }
    33.                 }
    34.             }
    35.         });
    36.     }
    Problem is in Android the Task stalls indefinitely - No exceptions or errors are thrown either.
    Any ways to fix this, or just another approach to do the same would be great.

    I tried using UnityEngine.Ping instead but it does not have a method to identify failed Pings.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,836
    Does it work single threaded? Try that first to see if the issue is with multithreading or the ping code.
     
  3. kancsa

    kancsa

    Joined:
    Feb 2, 2022
    Posts:
    9
    Yes, same issue single threaded, works only on the editor and in Android it errors out without showing any Exception.
    I managed to use UnityEngine.Ping though, if the ping is successful then ping.time is greater than 0.

    Thanks for the reply