private void StartListening() { Socket serverSocket = null; EndPoint endPoint = new IPEndPoint(IPAddress.Any, port); try { serverSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Blocking = true; serverSocket.Bind(endPoint); serverSocket.Listen(queueSize); log.Info("server started, listening on port " + port); running = true; stopped = false; while (running) { // Calling socket.Accept blocks the thread until the next incoming connection, // making difficult to stop the server from another thread. // The Poll always returns after the specified delay elapsed, or immeidately // returns if it detects an incoming connection. It's the perfect method // to make this loop regularly che the running var, ending gracefully // if requested. if (serverSocket.Poll(SocketPollMicroseconds, SelectMode.SelectRead)) { Socket clientSocket = serverSocket.Accept(); log.Info("new request received"); ConversionRequest connection = new ConversionRequest(clientSocket, converter); Thread clientThread = new Thread(new ThreadStart(connection.Run)); clientThread.Start(); } } } catch (Exception e) { log.Error("exception", e); } finally { if (serverSocket != null) serverSocket.Close(); running = false; stopped = true; log.Info("server stopped"); } }
private void StartListening() { Socket serverSocket = null; EndPoint endPoint = new IPEndPoint(IPAddress.Any, port); try { serverSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Blocking = true; serverSocket.Bind(endPoint); serverSocket.Listen(queueSize); log.Info("server started, listening on port " + port); running = true; stopped = false; while (running) { // Calling socket.Accept blocks the thread until the next incoming connection, // making difficult to stop the server from another thread. // The Poll always returns after the specified delay elapsed, or immediately // returns if it detects an incoming connection. It's the perfect method // to make this loop regularly check the running var, ending gracefully // if requested. if (serverSocket.Poll(SocketPollMicroseconds, SelectMode.SelectRead)) { Socket clientSocket = serverSocket.Accept(); Interlocked.Increment(ref connectionsCounter.value); ConversionRequest connection = new ConversionRequest(clientSocket, converter, connectionsCounter); // Creating a single thread for every connection has huge costs, // so I leverage the .NET internal thread pool ThreadPool.QueueUserWorkItem(connection.Run); } } } catch (Exception e) { log.Error("exception", e); } finally { if (serverSocket != null) serverSocket.Close(); running = false; stopped = true; log.Info("server stopped ("+ connectionsCounter.value + " connections still open)"); } }
private void StartListening() { Socket serverSocket = null; EndPoint endPoint = new IPEndPoint(IPAddress.Any, port); try { serverSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Blocking = true; serverSocket.Bind(endPoint); serverSocket.Listen(queueSize); log.Info("server started, listening on port " + port); running = true; stopped = false; while (running) { // Calling socket.Accept blocks the thread until the next incoming connection, // making difficult to stop the server from another thread. // The Poll always returns after the specified delay elapsed, or immediately // returns if it detects an incoming connection. It's the perfect method // to make this loop regularly check the running var, ending gracefully // if requested. if (serverSocket.Poll(SocketPollMicroseconds, SelectMode.SelectRead)) { Socket clientSocket = serverSocket.Accept(); Interlocked.Increment(ref connectionsCounter.value); ConversionRequest connection = new ConversionRequest(clientSocket, converter, connectionsCounter); // Creating a single thread for every connection has huge costs, // so I leverage the .NET internal thread pool ThreadPool.QueueUserWorkItem(connection.Run); } } } catch (Exception e) { log.Error("exception", e); } finally { if (serverSocket != null) { serverSocket.Close(); } running = false; stopped = true; log.Info("server stopped (" + connectionsCounter.value + " connections still open)"); } }
private void StartListening() { Socket serverSocket = null; EndPoint endPoint = new IPEndPoint(IPAddress.Any, port); try { serverSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Blocking = true; serverSocket.Bind(endPoint); serverSocket.Listen(queueSize); log.Info("server started, listening on port " + port); running = true; stopped = false; while (running) { // Calling socket.Accept blocks the thread until the next incoming connection, // making difficult to stop the server from another thread. // The Poll always returns after the specified delay elapsed, or immeidately // returns if it detects an incoming connection. It's the perfect method // to make this loop regularly che the running var, ending gracefully // if requested. if (serverSocket.Poll(SocketPollMicroseconds, SelectMode.SelectRead)) { Socket clientSocket = serverSocket.Accept(); log.Info("new request received"); ConversionRequest connection = new ConversionRequest(clientSocket, converter); Thread clientThread = new Thread(new ThreadStart(connection.Run)); clientThread.Start(); } } } catch (Exception e) { log.Error("exception", e); } finally { if (serverSocket != null) { serverSocket.Close(); } running = false; stopped = true; log.Info("server stopped"); } }