title: Java Server: Socket sending HTML code to browser
body: <p>I am trying to write a simple Java program using <code>ServerSockets</code> that will send some HTML code to the browser. Here is my code:</p>
  
  <pre><code>ServerSocket serverSocket = null;
  try {
      serverSocket = new ServerSocket(55555); 
  } catch (IOException e) {
      System.err.println("Could not listen on port: 55555.");
      System.exit(1);
  }
  
  Socket clientSocket = null; 
  try {
      clientSocket = serverSocket.accept();
  
      if(clientSocket != null) {           
          System.out.println("Connected");
      }
  } catch (IOException e) {
      System.err.println("Accept failed.");
      System.exit(1);
  }
  
  PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
  
  
  out.println("HTTP/1.1 200 OK");
  out.println("Content-Type: text/html");
  out.println("\r\n");
  out.println("&lt;p&gt; Hello world &lt;/p&gt;");
  out.flush();
  
  out.close();
  
  clientSocket.close();
  serverSocket.close();
  </code></pre>
  
  <p>I then go to <code>localhost:55555</code> in my browser and nothing displays. I know the connection is working because the program outputs "Connected" as checked in the if statement. I have also tried outputting the data from the inputStream and that works. But the text I am trying to output in the browser is not displaying at all, the program finishes running and I get a </p>
  
  <blockquote>
    <p>"Problem loading page - the connection has been reset"</p>
  </blockquote>
  
  <p>in my browser, but no text.</p>
  
  <p>I have searched the internet and it seems everyone else coding it this way is having their text display fine, they are having other problems.
  How can I fix this? </p>