| | |
| | | ip = _ip; |
| | | port = _port; |
| | | onConnected = _onConnected; |
| | | Dns.BeginGetHostAddresses(_ip, OnGetHostAddresses, null); |
| | | //目前测试到异步两个问题 |
| | | // 1. BeginGetHostAddresses 不明情况下会很久才回调,导致触发超时 |
| | | // 2. 超时的情况下多次尝试登录后,会触发多次OnGetHostAddresses,导致登录异常 |
| | | //Dns.BeginGetHostAddresses(_ip, OnGetHostAddresses, null); |
| | | |
| | | IPAddress ipAddress; |
| | | #if UNITY_IPHONE |
| | | IPHostEntry ipAddresses = Dns.GetHostEntry(_ip); |
| | | ipAddress = ipAddresses.AddressList[0]; |
| | | |
| | | #else |
| | | IPAddress[] ipAddresses = Dns.GetHostAddresses(_ip); |
| | | ipAddress = ipAddresses[0]; |
| | | #endif |
| | | |
| | | |
| | | if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) |
| | | { |
| | | DebugEx.Log("当前使用的网络: IPV6"); |
| | | m_Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); |
| | | } |
| | | else |
| | | { |
| | | DebugEx.Log("当前使用的网络: IPV4"); |
| | | m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
| | | } |
| | | |
| | | var ipEndPoint = new IPEndPoint(ipAddress, port); |
| | | if (ipEndPoint == null) |
| | | { |
| | | Debug.Log("IpEndPoint is null"); |
| | | } |
| | | |
| | | m_Socket.BeginConnect(ipEndPoint, new AsyncCallback(ConnectCallBack), null); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | DebugEx.LogError(e.Message); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | private void OnGetHostAddresses(IAsyncResult _result) |
| | |
| | | /// </summary> |
| | | public void CloseConnect() |
| | | { |
| | | Debug.Log("==== CloseConnect"); |
| | | try |
| | | { |
| | | isStopTreading = true; |