title: Trouble sending HTTP response with Java Socket
body: <p>I've been trying to write the beginnings of a simple web server, but can't seem to get the response to get sent.  I've tried every type of Output stream imaginable but nothing seems to work.  I'm at a loss.  Here are the two classes I'm using, sorry about some of the extraneous code:</p>
  
  <pre><code>package edu.xsi.webserver;
  
  import java.io.IOException;
  import java.net.ServerSocket;
  
  
  public class WebServer {
  
  int port;
  ServerSocket server;
  
  public WebServer(int port) throws IOException{
  
      this.port = port;
      this.server = new ServerSocket(port);
      Thread t = new Thread(new ServerExec());
      t.start();
  
  }
  
  public class ServerExec implements Runnable {
  
      public void run(){
  
          int i = 0;
          while (true) {
  
              try {
                  new WebSession(server.accept(), i++);
                  System.out.println("Should print before session");
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
          }
      }
  
  
  }
  
  public static void main(String[] args) {
  
      try {
          WebServer webServer = new WebServer(8888);
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>This is the session class that handles the response.</p>
  
  <pre><code>package edu.xsi.webserver;
  
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.net.Socket;
  import java.util.Scanner;
  
  public class WebSession implements Runnable {
  
  Socket client;
  int num;
  
  public WebSession(Socket client, int num) {
  
      this.num = num;
      this.client = client;
      Thread t = new Thread(this);
      t.start();
  }
  
  
  public void run() {
  
      Scanner s = null;
      DataOutputStream out = null;
  
      try {
  
          s = new Scanner(client.getInputStream());
          out = new DataOutputStream(client.getOutputStream());
  
          //Get all input from header
          while (s.hasNextLine()) {
              System.out.println(s.nextLine());
          }
          out.writeBytes("HTTP/1.1 200 OK\r\n");
          out.writeBytes("Content-Type: text/html\r\n\r\n");
          out.writeBytes("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Hello&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;");
  
          s.close();
          out.flush();
          out.close();
  
      } catch(IOException ioe) {
  
          ioe.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>}</p>