unity 网络ipv6连接


public class IPv6SupportMidleware
{
#if UNITY_IPHONE && !UNITY_EDITOR
	[DllImport("__Internal")]
	private static extern string getIPv6(string mHost, string mPort);  
#endif

    //"192.168.1.1&&ipv4"
    private static string GetIPv6(string mHost, string mPort)
    {
#if UNITY_IPHONE && !UNITY_EDITOR
		string mIPv6 = getIPv6(mHost, mPort);
		return mIPv6;
#else
        return mHost + "&&ipv4";
#endif
    }

    public static void getIPType(String serverIp, String serverPorts, out String newServerIp, out AddressFamily mIPType)
    {
        mIPType = AddressFamily.InterNetwork;
        newServerIp = serverIp;
        try
        {
            string mIPv6 = GetIPv6(serverIp, serverPorts);
            if (!string.IsNullOrEmpty(mIPv6))
            {
                string[] m_StrTemp = System.Text.RegularExpressions.Regex.Split(mIPv6, "&&");
                if (m_StrTemp != null && m_StrTemp.Length >= 2)
                {
                    string IPType = m_StrTemp[1];
                    if (IPType == "ipv6")
                    {
                        newServerIp = m_StrTemp[0];
                        mIPType = AddressFamily.InterNetworkV6;
                    }
                }
            }
        }
        catch (Exception e)
        {
            UIUtils.Log("GetIPv6 error:" + e);
        }

    }

}


    /// 
    /// 建立服务器连接
    /// 
    public void SocketConnection()
    {
        try
        {
            if (mSocket != null || string.IsNullOrEmpty(ip)) {
                isReconnect = false;
                return;
            }
            allPackages.Clear();
            //sendList.Clear();
            CloseSocket();
            string newServerIp = "";
            AddressFamily newAddressFamily = AddressFamily.InterNetwork;
            IPv6SupportMidleware.getIPType(ip, PORT.ToString(), out newServerIp, out newAddressFamily);
            mSocket = new TcpClient(newAddressFamily);
            //防止延迟,即时发送!
            mSocket.NoDelay = true;

            UIUtils.Log("Connect IP::" + newServerIp);
            mSocket.BeginConnect(newServerIp, PORT, new AsyncCallback(ConnectCallBack), mSocket);
            _gameNetWorkReachability = Application.internetReachability;

            return;
        }
        catch (Exception e)
        {
            UIUtils.Log(e.ToString());
            CloseSocket();
            OpenTipNetError();
            return;
        }
    }
 
  
 
 

你可能感兴趣的:(unity 网络ipv6连接)