title: Confused with input/output streams for a proxy
body: <p>Attempting to code a very simple, bare bones threaded HTTP proxy server in Java, and 
  I've made good progress:  </p>
  
  <ol>
  <li><p>The client connects to the server, input and output
  streams to the client are made</p></li>
  <li><p>The target URL is extracted from the GET request</p></li>
  <li><p>A connection is made with the target web server, and an inputstream is established</p></li>
  </ol>
  
  <p>Now I understand I need to send the data coming in from the server inputstream and forward
  it to the client outputstream, but how to exactly do this escapes me.  The extend of my experience
  with input and output streams has been sending single Strings, and I don't quite have a solid
  understanding of how exactly these streams work.</p>
  
  <p>Am I supposed to read all the data from the inputstream into a file, and then write to the output stream?  Is there a more elegant solution?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  
  public class Proxy {
  
  /////////////////////////////////////
  public static void go(){
  
  int incomingport = 50000;
  ServerSocket myproxysocket = null;
  
  try{
  myproxysocket = new ServerSocket(incomingport);
  while(true){
  System.out.println("HTTP Java Proxy Server");
  System.out.println();
  System.out.println("Listening on port 50000...Press CTRL+C to exit");
  System.out.println();
  Socket client = myproxysocket.accept();
  Thread t = new Thread(new ClientHandler(client));
  t.start();
  }//end while
  }//end try
  
  catch(Exception E){
  E.printStackTrace();    
  }
  }//end method go()
  
  ///////////////////////////////////
  public static void main(String args[]) {
  
  Proxy myserver =  new Proxy();
  myserver.go();
  
  }//end main
  }//end class proxy
  
  
  /////////////////////////////////////////////////////////////////////////////////////
  class ClientHandler implements Runnable{
  
  private final DataInputStream clientin;
  private final DataOutputStream clientout;
  private final Socket clientSocket;
  
  
  //constructor----------------
  public ClientHandler(Socket incomingSocket) throws IOException{
  
  clientSocket = incomingSocket; 
  clientin = new DataInputStream(incomingSocket.getInputStream());
  clientout = new DataOutputStream(incomingSocket.getOutputStream());
  
  
  }//--------------------------
  
  ////////////////////
  public void run(){
  try{
  
  //Get URL from client
  String getrequestline = clientin.readLine();
  String targetURL = null;
  String clientIP = null;
  StringTokenizer st = new StringTokenizer(getrequestline);
  st.nextToken();
  targetURL = st.nextToken();
  clientIP = clientSocket.getRemoteSocketAddress().toString();
  System.out.println();
  System.out.println("IP: " + clientIP);
  System.out.println("Requested URL: " + targetURL);
  System.out.println();
  
  //connect to target URL
  URL url = new URL(targetURL);
  URLConnection targetconnection = url.openConnection();
  targetconnection.setDoInput(true);
  targetconnection.setDoOutput(false);
  
  
  InputStream is = targetconnection.getInputStream();
  
  //How do I read in from is and write out to clientout?
  
  //clientout.write(???????)
  
  }//try
  catch(Exception E){
   E.printStackTrace();
  }//catch
  finally{
  try{clientSocket.close();}
  catch(Exception ee){}
  }//finally
  }//end run----------
  
  }//end CLASS--------------------------------------------------------------------------
  </code></pre>