public override void StartListening(int port) { if (IsListening) { throw new InvalidOperationException("Should stop listening before restarting."); } // Establish the local endpoint for the socket. var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); var ipAddress = ipHostInfo.AddressList[0]; var localEndPoint = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. var listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint listener.Bind(localEndPoint); listener.Listen(100); // I'm not sure about thread-safety of this assignment. // Let's just hope code above executes quickly enough to prevent problems listeningSocet = listener; ListeningStateChanged?.Invoke(this, EventArgs.Empty); Handle(); }
public void Start(int MaxConnectionQueue = 25) { if (Running) { throw new InvalidOperationException("Listener is already running."); } else { this.MaxConnectionQueue = MaxConnectionQueue; _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _listener.Bind(new IPEndPoint(0, Port)); _listener.Listen(MaxConnectionQueue); Running = true; ListeningStateChanged?.Invoke(this, Running); _listener.BeginAccept(AcceptCallBack, null); } }
public void Stop() { if (Listening) { listeningSocket.Close(); foreach (var client in connectedClients) { client.Close(); } connectedClients.Clear(); BlockedHosts.Clear(); Listening = false; ListeningStateChanged?.Invoke(this, Listening); } else { throw new InvalidOperationException("Server isn't listening. Please call Listen() before calling Close()"); } }
public override void StopListening() { if (!IsListening) { throw new InvalidOperationException("Should start listening before stopping."); } try { listeningSocet.Close(); } catch (Exception e) { MessageBox.Show(e.ToString(), "Error"); } listeningSocet = null; ListeningStateChanged?.Invoke(this, EventArgs.Empty); }
public void Stop() { if (Running) { _listener.Close(); _listener = null; foreach (var client in _connectedClients) { client.Dispose(); } _connectedClients.Clear(); MaxConnectionQueue = 0; Running = false; ListeningStateChanged?.Invoke(this, Running); } else { throw new InvalidOperationException("Listener isn't running."); } }
public void Listen() { if (Listening) { throw new InvalidOperationException("Server is already listening. Please call Close() before calling Listen()"); } else { if (ServerOptions.ListenEndPoint == null) { throw new InvalidOperationException("Please specify a endpoint to listen on before calling Listen()"); } listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listeningSocket.Bind(ServerOptions.ListenEndPoint); listeningSocket.Listen(ServerOptions.MaxConnectionQueue); Listening = true; ListeningStateChanged?.Invoke(this, Listening); listeningSocket.BeginAccept(AcceptCallBack, null); } }