/// <summary> /// Connect to remote host /// </summary> /// <param name="point">Remote end point</param> public void Connect(IPEndPoint point) { try { if (ClientType == NetworkType.Tcp) { Socket = new Socket(point.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Socket.BeginConnect(point, new AsyncCallback(ConnectCallback), null); } else if (ClientType == NetworkType.Udp) { Socket = new Socket(point.AddressFamily, SocketType.Dgram, ProtocolType.Udp); Socket.Connect(point); Socket.Send(new byte[] { 0 }); OnConnected?.Invoke(this, new EventArgs()); UDPTimer.Start(); DisconnectTimer.Start(); Socket.BeginReceive(Buffer, 0, BufferSize, 0, new AsyncCallback(ReadCallbackUDP), null); } } catch (SocketException se) { OnError?.Invoke(this, new ErrorEventArgs(se)); } }
/// <summary> /// Disconnect from remote host /// </summary> public void Disconnect() { try { if (Socket == null) { return; } UDPTimer.Stop(); DisconnectTimer.Stop(); Socket.Close(); OnDisconnected?.Invoke(this, new EventArgs()); } catch (SocketException se) { OnError?.Invoke(this, new ErrorEventArgs(se)); } }