LoginSignup
6
7

More than 5 years have passed since last update.

Unity 通信状態の取得

Posted at

■参考URL
https://msdn.microsoft.com/ja-jp/library/system.net.dns.gethostaddressesasync(v=vs.110).aspx
https://forum.unity.com/threads/use-dns-addresses-for-networking.199304/

■呼び元処理

    /// <summary>
    /// Pingによる応答時間の取得
    /// </summary>
    public async void OnClickGetPingTime()
    {
        int pingTime = await PingStatus.GetPingTime();
    }

■呼び先処理(PINGにより応答時間取得)

    /// <summary>
    /// 通信状態の取得クラス
    /// </summary>
    public static class PingStatus
    {
        /// <summary>
        /// PING応答時間の取得
        /// </summary>
        /// <returns>PING応答時間(msec)</returns>
        public static async Task<int> GetPingTime()
        {
            // "/"を省いたホスト名にする事
            string host = "";

            // ホスト名からIPアドレスを非同期で取得
            IPAddress[] serverIPs = await Dns.GetHostAddressesAsync(host);

            // コンストラクタが走った時点でPING送信される
            Ping ping = new Ping(serverIPs[0].ToString());

            await new WaitUntil(() => ping.isDone == true);

            return ping.time;
        }
    }
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7