Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Ping specific an address

Discussion in 'Scripting' started by Joishi, Jun 17, 2015.

  1. Joishi

    Joishi

    Joined:
    May 9, 2015
    Posts:
    7
    Hi, this is my first post in this forum, I´m not a native speaker, so, excuse me if I make some mistakes or misspelled words.
    I've been trying to do a ping to an specific address, but I couldn't make it. I tried to find a previous post about this topic, but I couldn't find it. I really will appreciate for any help.


    Code (CSharp):
    1.  
    2. public string direccion;
    3.  
    4. void Update () {
    5.      Ping pinger = new Ping (direccion);
    6.      tiempo= pinger.time;
    7.   print(""+tiempo);
    8.        
    9.    }
    10.  
    Note: I know, that the string is a IP address, I´m testing using 216.58.192.100, a google address.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you probably don't want that code in an update, you'll be creating a new ping every frame! :eek:

    from the api scripting page (http://docs.unity3d.com/ScriptReference/Ping.html) the ping is "asynchronous" i.e. it starts doing "it's thing" when you create it, but it might not be done and able to return a time until a little while later.

    So you want to create 1 ping object (just one, not one per frame!), then check the "isDone" (http://docs.unity3d.com/ScriptReference/Ping-isDone.html) to see if it has come back with a time yet.
     
  3. Joishi

    Joishi

    Joined:
    May 9, 2015
    Posts:
    7
    Actually I don´t want to do a ping each frame, this code is just for a test, The idea is to create a button to test the delay between server and client. I´m already check for the documentation you linked(thanks by the way), but the issue is to know why when run the code, the result is just "-1", and the isDone "false".

    Note:I tried the same code in the start function, I just copied the last code I tried.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the ping isn't going to have a value immediately in the same frame. Create the ping, check for isDone in update, when isDone is true access the time.
     
  5. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Try something like this?

    Code (CSharp):
    1. public void CheckPing(string ip)
    2.         {
    3.             StartCoroutine(StartPing(ip));
    4.         }
    5.  
    6.         IEnumerator StartPing(string  ip)
    7.         {
    8.             WaitForSeconds f = new WaitForSeconds(0.05f);
    9.             Ping p = new Ping(ip);
    10.             while (p.isDone == false)
    11.             {
    12.                 yield return f;
    13.             }
    14.             PingFinished(p);
    15.         }
    16.  
    17.  
    18.         public void PingFinished(Ping p)
    19.         {
    20.             // stuff when the Ping p has finshed....
    21.         }
    Call CheckPing once and wait for pingfinished to be called
     
    Joishi likes this.
  6. Joishi

    Joishi

    Joined:
    May 9, 2015
    Posts:
    7
    Thanks, that was de problem, I was thinking in use the IEnumarator, But actually I didnt do it.
    Solved.