public static void GetIpType(string serverIp, string serverPort, out string newServerIp, out AddressFamily newServerAddressFamily) { newServerAddressFamily = 2; newServerIp = serverIp; try { string iPv = CompatibilityIP.GetIPv6(serverIp, serverPort); if (!string.IsNullOrEmpty(iPv)) { string[] array = Regex.Split(iPv, "&&"); if (array.Length >= 2) { string text = array[1]; if (text == "ipv6") { newServerIp = array[0]; newServerAddressFamily = 23; } } } } catch (Exception ex) { Debug.LogError(ex.get_Message()); } }
void InitSocket() { Close(); #if NETFX_CORE socket = new Socket(); try { socket.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), socket); } catch (Exception e) { Debug.LogError(ClaName + e.ToString() + "-----BegainConnectException"); } #elif UNITY_IPHONE// && !UNITY_EDITOR try { string serverIp; AddressFamily ipType; CompatibilityIP.GetIpType("mokaserver01.baobaolong.club", "21001", out serverIp, out ipType); Debug.Log("\n"); Debug.Log("LOG_1_serverIp:" + serverIp + " ipType:" + ipType); socket = new Socket(ipType, SocketType.Stream, ProtocolType.Tcp); Debug.Log("LOG_2_socket:" + (socket == null).ToString()); IPAddress address = IPAddress.Parse(serverIp); IPEndPoint point = new IPEndPoint(address, 21001); Debug.Log("LOG_3_address:" + address.ToString() + " point:" + point); try { socket.BeginConnect(point, new AsyncCallback(ConnectCallback), socket); } catch (Exception e) { Debug.LogError(ClaName + e.ToString() + "-----BegainConnectException"); } } catch (Exception e1) { Debug.Log("LOG_4_IPV6形式的创建Socket失败,现使用常规方式创建Socket..."); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), socket); } catch (Exception e2) { Debug.LogError(ClaName + e2.ToString() + "-----BegainConnectException"); } } #else socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.BeginConnect(ip, port, new AsyncCallback(ConnectCallback), socket); } catch (Exception e) { Debug.LogError(ClaName + e.ToString() + "-----BegainConnectException"); } #endif }