title: Redirecting persistent HTTP requests and responses using sockets
body: <p>I wish to create a router of some sort to redirect HTTP requests from an HTTP client to a servlet (they preform a negotation process. More background: I wish to do an auth from windows to windows server, going throug a redirective Unix web server).</p>
  
  <p>My servlet is on <code>http://localhost:8080</code>
  and my redirector is on <code>8081</code></p>
  
  <p>So, I wrote this:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Redirect {
  
      /**
       * @param args
       * @throws IOException
       * @throws InterruptedException 
       */
      public static void main(String[] args) throws IOException, InterruptedException {
          ServerSocket ss = new ServerSocket(8081);
          Socket s = new Socket("localhost", 8080);
          Socket l = ss.accept();
          l.setKeepAlive(true);
          s.setKeepAlive(true);
          OutputStream os = s.getOutputStream();
          InputStream is = l.getInputStream();
          Thread t1 = new Thread(new MyReader(is, os,"#1"));
          t1.start();
          InputStream is2 = s.getInputStream();
          OutputStream os2 = l.getOutputStream();
          Thread t2 = new Thread(new MyReader(is2, os2,"#2"));
          t2.start();
      }
  
      public static class MyReader implements Runnable {
          private InputStream _i;
          private OutputStream _o;
          private String _id;
  
          public MyReader(InputStream i, OutputStream o, String id) {
              _i = i;
              _o = o;
              _id = id;
          }
  
          @Override
          public void run() {
              try {
                  int x;
                  x = _i.read();
                  while (x != -1) {
                      System.out.println(_id);
                      _o.write(x);
                      _o.flush();
                      x = _i.read();
                  }
                  System.out.println(x);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>It works fine for most servlets! So what's my problem? My servlet is a persistent one <a href="http://en.wikipedia.org/wiki/HTTP_persistent_connection" rel="nofollow">http://en.wikipedia.org/wiki/HTTP_persistent_connection</a>, that is it does connection=keep-alive and does a whole back and forth messaging dance.</p>
  
  <p>What am I doing wrong? I thought I could run two MyReader threads and that they will block until new information comes, but instead they keep blocking.</p>
  
  <p>This is my client:</p>
  
  <pre><code>public class Client {
      public static void main(String[] args) throws IOException {
          URL u = new URL("http://localhost:8081/Abc/Def");
          HttpURLConnection huc = (HttpURLConnection)u.openConnection();
          huc.setRequestMethod("GET");
          huc.setDoOutput(true);
          huc.connect();
          InputStream is = huc.getInputStream();//The all auth process is done here
               //and HttpUrlConnection support it. If I change 8081 to 8080, it works
  
  }
  </code></pre>