Unity Ping IPV6

unity的System.Net.NetworkInformation.Ping不能正常使用调用sendaysnc直接卡死主线程 timeout时间后回调PingCompletedEventArgs e.Reply = null所以只好使用Unity自带ping 自带的ping测试ip v6不能直接传ipv6地址 要通过dns路由获取host对应的地址

public class NetUtil
    {
        /// 
        /// host to ip
        /// 
        /// 
        public static string ResolveHostNameToIP(string host, out AddressFamily af)
        {
            af = AddressFamily.Unknown;
            IPAddress[] AddressList = null;
            try
            {
                AddressList = Dns.GetHostAddresses(host);
            }
            catch (SocketException)
            {
                AddressList = new IPAddress[] { };
            }
            foreach (var ip in AddressList)
            {
                af = ip.AddressFamily;
                //IPv4
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    byte[] ipb = ip.GetAddressBytes();
                    StringBuilder sb = new StringBuilder();
                    foreach (var b in ipb)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(".");
                        }
                        sb.Append(b);
                    }
                    return sb.ToString();
                }
                //IPv6
                if (ip.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    byte[] ipb = ip.GetAddressBytes();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < ipb.Length; ++i)
                    {
                        if (i % 2 == 0)
                        {
                            if (sb.Length > 0)
                            {
                                sb.Append(":");
                            }
                        }
                        sb.Append(ipb[i].ToString("x2"));
                    }
                    return sb.ToString();
                }
            }
            af = AddressFamily.Unknown;
            return string.Empty;
        }
    }
public class UnityPing
    {
        private Ping _ping;
        public UnityPing(string host, float timeout, Action callback)
        {
            AddressFamily af = AddressFamily.Unknown;
            var ip = NetUtil.ResolveHostNameToIP(host, out af);
            _ping = new Ping(ip);
            AppFacade.Instance.GetBaseManager(ManagerName.BaseManger)
                .StartCoroutine(PingTimeOut(Time.unscaledTime, timeout, callback));
        }
        private IEnumerator PingTimeOut(float unscaledTime, float timeout, Action callback)
        {
            while (true)
            {
                if (null == _ping)
                {
                    yield break;
                }
                if (!_ping.isDone)
                {
                    yield return Wait.ForEndOfFrame;
                }
                break;
            }
            callback(Time.unscaledTime - unscaledTime);
        }
        public void DestroyPing()
        {
            if (null != _ping)
            {
                _ping.DestroyPing();
                _ping = null;
            }
        }
    }

你可能感兴趣的:(Unity Ping IPV6)