Search Unity

Need help returning latency from ping(ip)

Discussion in 'Scripting' started by syntaxjedi, Jul 4, 2020.

  1. syntaxjedi

    syntaxjedi

    Joined:
    Dec 19, 2015
    Posts:
    1
    Hi all, i'm having some trouble getting a server response from my code when pinging my servers. I set up a script that looks to three servers and returns their response times, two servers are unreachable and return a 0 properly but the one server that is reachable doesn't give any feedback in the script attached below. I'm expecting an output of the
    ping.time
    but only receive outputs from the unreachable servers. Can anybody help explain what i've done wrong?

    Code (CSharp):
    1. public void Start()
    2.         {
    3.             StartCoroutine(pingServer(SAdress));
    4.         }
    5.  
    6.         public IEnumerator pingServer(string ip)
    7.         {
    8.             Ping p = new Ping(ip);
    9.             while (!p.isDone) yield return null;
    10.  
    11.             displayPing(p);
    12.         }
    13.  
    14.         public void displayPing(Ping p)
    15.         {
    16.             Debug.Log($"server ping: {p.time}");
    17.          
    18.             if(p.time == 0)
    19.             {
    20.                 foreach(GameObject bar in LatencyUI)
    21.                 {
    22.                     bar.GetComponent<Image>().color = new Color32(60, 60, 60, 255);
    23.                 }
    24.             }
    25.             else
    26.             {
    27.                 JoinButton.onClick.AddListener(() => serverManager.ConnectToServer(this));
    28.             }
    29.         }
    the output i get from running this shows
    server ping: 0
    for the two down servers and none of the join buttons are activated.

    edit:
    upon further review, i am an idiot sandwich and didn't realize i had the servers backwards. the down servers time out and the ones with fast response times properly reply in the console. keeping this up incase someone else could use help with the code.
     
    Last edited: Jul 4, 2020