title: Self created http server cannot handle postman request
body: <p>I'm trying to implement a simple http server in Java to understand how does a http server work. Now, I'm able to send request from any browser and receive the proper response, however, when I try to simulate a request from Postman, it always throws <code>java.net.SocketException: Broken pipe (Write failed)</code> exception.</p>
  
  <p>My http server is very simple: once received a request, echo a message to sender.</p>
  
  <p>The implementation is as follow, <a href="https://github.com/neesonqk/HttpServer" rel="nofollow noreferrer">Source Code</a></p>
  
  <pre><code>package com.ont.http;
  
  import java.io.*;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class SingleThreadHttpServer implements HttpServer {
  
      public void run(int port) throws IOException {
          ServerSocket serverSocket = new ServerSocket(port);
          try {
              while (true) {
                  Socket socket = serverSocket.accept();
  
                  BufferedReader inBufferReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                  StringBuilder stringBuffer = new StringBuilder();
  
                  String inputLine;
                  while ((inputLine = inBufferReader.readLine()) != null &amp;&amp; !inputLine.equals("")) {
                      stringBuffer.append(inputLine);
                      stringBuffer.append("\r\n");
                  }
  
                  System.out.println(stringBuffer.toString());
  
                  OutputStream outStream = socket.getOutputStream();
                  BufferedReader bufferedReader = new BufferedReader(new StringReader("A Message from server."));
  
                  // Header should be ended with '\r\n' at each line.
                  outStream.write("HTTP/1.1 200 OK\r\n".getBytes());
                  outStream.write("Main: OneServer 0.1\r\n".getBytes());
                  outStream.write("Content-length: 22\r\n".getBytes()); // if text/plain the length is required
                  outStream.write("Content-Type: text/plain\r\n".getBytes());
  
                  // An empty line is required after the header
                  outStream.write("\r\n".getBytes());
  
                  String line;
                  while ((line = bufferedReader.readLine()) != null) {
                      outStream.write(line.getBytes());
                  }
  
                  inBufferReader.close();
                  bufferedReader.close();
                  outStream.flush();
                  outStream.close(); // Socket will close automatically once output stream is closed.
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Any types of request sent from Postman will immediately trigger that exception, and debug tells that the socket connection had been lost during <code>outStream.write(...)</code>. if I send from a browser, it never trigger that problem. </p>
  
  <p>The thing I don't understand is that there shouldn't be any setting applied to Postman, because when I try to simulate a request to my tomcat, it handles everything like a charm, there must be something gone wrong with my code but I don't know where and why. </p>
  
  <p>Any helps or tips would be appreciated, thank you.</p>
  
  <p>----- Updates ----</p>
  
  <p>From Postman console, I can see this exception,</p>
  
  <pre><code>Error: read ECONNRESET
  Request Headers:
  accept:"text/plain"
  cache-control:"no-cache"
  </code></pre>
  
  <p>If I left only <code>outStream.write("HTTP/1.1 200 OK\r\n".getBytes())</code> this line, and delete all other <code>outStream.write...</code>, both browsers and postman work like a charm. this is really pulling my hair, but why?</p>