protected virtual void OnConnect(IAsyncResult result) { var socket = (Socket)result.AsyncState; try { socket.EndConnect(result); ConnectTime = DateTime.Now; SocketLog("OnConnect SUCCESS!"); OnConnectCallback?.Invoke(true, null); } catch (Exception e) { SocketLogError("OnConnect Failed:" + e.Message); OnConnectCallback?.Invoke(false, e.Message); } }
public void Connect(string ip, int port) { m_IP = ip; m_Port = port; IsUse = true; m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Socket.SendBufferSize = BUF_SIZE; m_Socket.ReceiveBufferSize = BUF_SIZE; m_Socket.SendTimeout = 3000; m_Socket.ReceiveTimeout = 3000; var addr = IPAddress.Parse(m_IP); var endPoint = new IPEndPoint(addr, m_Port); try { m_Socket.BeginConnect(endPoint, OnConnect, m_Socket); } catch (Exception e) { SocketLogError("Connect ERROR:" + e.Message); OnConnectCallback?.Invoke(false, e.Message); } }