public void StartListening() { try { this.listener = new TcpListener(IPAddress.Any, this.Port); this.Prefix = new Uri(string.Format(CultureInfo.InvariantCulture, "http://localhost:{0}", this.Port)); this.dispatcher = new UriDispatchTables(new Uri(this.Prefix, UrnPrefix)); this.executorDispatcher = new CommandExecutorDispatchTable(); // Start listening for client requests. this.listener.Start(); // Enter the listening loop while (true) { Logger.Debug("Waiting for a connection..."); // Perform a blocking call to accept requests. var client = this.listener.AcceptTcpClient(); // Get a stream object for reading and writing using (var stream = client.GetStream()) { stream.ReadTimeout = 60 * 1000; HttpRequest acceptedRequest; try { acceptedRequest = HttpRequest.ReadFromStreamWithoutClosing(stream); } catch (IOException ex) { Logger.Error("Error occured while reading request: {0}", ex); client.Close(); Logger.Debug("Client closed\n"); continue; } Logger.Debug("ACCEPTED REQUEST {0}", acceptedRequest.StartingLine); var response = this.HandleRequest(acceptedRequest); using (var writer = new StreamWriter(stream)) { try { writer.Write(response); writer.Flush(); } catch (IOException ex) { Logger.Error("Error occured while writing response: {0}", ex); } } // Shutdown and end connection } client.Close(); Logger.Debug("Client closed\n"); } } catch (SocketException ex) { Logger.Error("SocketException occurred while trying to start listner: {0}", ex); throw; } catch (ArgumentException ex) { Logger.Error("ArgumentException occurred while trying to start listner: {0}", ex); throw; } finally { // Stop listening for new clients. this.listener.Stop(); } }
public void StartListening() { try { this.listener = new TcpListener(IPAddress.Any, this.Port); this.Prefix = new Uri(string.Format(CultureInfo.InvariantCulture, "http://localhost:{0}", this.Port)); this.dispatcher = new UriDispatchTables(new Uri(this.Prefix, UrnPrefix)); this.executorDispatcher = new CommandExecutorDispatchTable(); // Start listening for client requests. this.listener.Start(); // Enter the listening loop while (true) { Logger.Debug("Waiting for a connection..."); // Perform a blocking call to accept requests. var client = this.listener.AcceptTcpClient(); //Detect connection close, this happens a lot on Windows 10 if (client.Client.Poll(0, SelectMode.SelectRead)) { byte[] buff = new byte[1]; if (client.Client.Receive(buff, SocketFlags.Peek) == 0) { var ep = (IPEndPoint)client.Client.RemoteEndPoint; Logger.Debug("Connection disconnected for IP: {0}/port: {1}", ep.Address, ep.Port); continue; } } // Get a stream object for reading and writing using (var stream = client.GetStream()) { var acceptedRequest = HttpRequest.ReadFromStreamWithoutClosing(stream); Logger.Debug("ACCEPTED REQUEST {0}", acceptedRequest.StartingLine); var response = this.HandleRequest(acceptedRequest); using (var writer = new StreamWriter(stream)) { try { writer.Write(response); writer.Flush(); } catch (IOException ex) { Logger.Error("Error occured while writing response: {0}", ex); } } // Shutdown and end connection } client.Close(); Logger.Debug("Client closed\n"); } } catch (SocketException ex) { Logger.Error("SocketException occurred while trying to start listner: {0}", ex); throw; } catch (ArgumentException ex) { Logger.Error("ArgumentException occurred while trying to start listner: {0}", ex); throw; } finally { // Stop listening for new clients. this.listener.Stop(); } }