0

Context

There is a simple self-described HTTP server that listens incoming connections. When I put ip_address:port in browser, I should get a simple HTTP page with text information. It works fine if server accepts incoming stream from clients, parse it and read headers.

But I need to realize the same without reading any incoming streams. So I hide 3 lines of code:

//inputStream = new BufferedStream(socket.GetStream());
//parseRequest();
//readHeaders();

Then I check the result with command:

telnet address port 

... and it works OK, server returns me the same text.

But in browser I get an error:

This site can’t be reached

... and in the browser`s command line there appears an ERROR:

GET http://adress:port net::ERR_CONNECTION_RESET

Question

How can I get response correctly, if I can not read HTTP request from client?

Code

public void listen()
 {
   listener = new TcpListener(adr, port);
   listener.Start();
    while (is_active)
    {
     TcpClient s = listener.AcceptTcpClient();
     HttpProcessor processor = new HttpProcessor(s, this);
     Thread thread = new Thread(new ThreadStart(processor.process));
     thread.Start();
     Thread.Sleep(1);
     }
  }

 public void process()
{
  outputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
  try {
      //inputStream = new BufferedStream(socket.GetStream());
      //parseRequest();
      //readHeaders();
      handleGETRequest();
      }
  catch (Exception e)
      {
       writeFailure();
       Library.WriteLog("Exception  " + e.ToString());
      }
       outputStream.Flush();
       outputStream = null; 
       socket.Close();
}

public void handleGETRequest()
{
 srv.handleGETRequest(this);
}

public override void handleGETRequest(HttpProcessor p)
{
  p.outputStream.WriteLine("HTTP / 1.1 200 OK");
  p.outputStream.WriteLine("Content-Type: text/html; charset=UTF-8");
  p.outputStream.WriteLine("Transfer-Encoding: chunked");
  p.outputStream.WriteLine("Connection: keep-alive");
  p.outputStream.WriteLine("Keep-Alive: timeout=2000");
  p.outputStream.WriteLine("Cache-Control: max-age=0\n");
  p.outputStream.WriteLine("<html>");
  p.outputStream.WriteLine("<head><title> 200 Test </title></head>");
  p.outputStream.WriteLine("<center><h1> 302 Test2 ");
  p.outputStream.WriteLine(Service1.GetData());
  p.outputStream.WriteLine("</h1></center>");
  p.outputStream.WriteLine("<hr><center> nginx </center>");
  p.outputStream.WriteLine("</body>");
  p.outputStream.WriteLine("</html>");
}
Maciej Jureczko
  • 1,560
  • 6
  • 19
  • 23
  • 1
    You definitely need to read the request since the RST gets generated by the OS if the socket is closed by the server while there are unread data from the client. Possible duplicate of [Ultra simple HTTP socket server, written in PHP, behaving unexpectedly](https://stackoverflow.com/questions/33701177), [Perl: Connection reset with simple HTTP server](https://stackoverflow.com/questions/2485282). – Steffen Ullrich Oct 18 '17 at 13:56
  • _"I need to realize the same without reading any incoming streams"_ - I highly doubt that, because then you wouldn't be an HTTP server anymore. You just spit your data back as soon as a client connects, and then close the socket? Why? – CodeCaster Oct 18 '17 at 15:01
  • Yes, because I need to get data from server equally in browser and in command line through telnet. And if server starts to reed incoming http request, then telnet wants some incoming data too. So telnet opens telnet session and doesn't return me information and doesn`t close connection while I send empty request. – Dmitriy Bilberry Oct 19 '17 at 05:42

0 Answers0