/// <summary> /// 异步连接服务器 /// </summary> public void Connect(IPEndPoint ipEndPoint) { if (_connectState != NetworkClientState.Closed) { if (onConnected != null) { onConnected.Invoke(); } Debug.Log("客户端非关闭状态,无法进行该操作!"); return; } _currentIpEndPoint = ipEndPoint; _connectState = NetworkClientState.Connecting; _unityInvoke = new UnityInvoke("SocketClient"); Debug.Log("connect Start"); try { _msgCounter = 0; _socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socket.BeginConnect(ipEndPoint, DoConnect, _socket); } catch (Exception ex) { Debug.Log(ex.ToString()); } }
public bool ResetSocket() { if (_connectState != NetworkClientState.Closed) { _connectState = NetworkClientState.Closed; try { if (_socket.Connected) { _socket.Shutdown(SocketShutdown.Both); _socket.Dispose(); } } finally { _socket.Close(); _socket = null; } _byteArray.Destroy(); _byteArray = null; return(true); } if (_unityInvoke != null) { _unityInvoke.Dispose(); _unityInvoke = null; } _isCalled = true; return(false); }