title: Reading Http Get request from a inputstream and sending it to the host. using sockets
body: <p>i have this assignment where i am supposed to write a proxy server that uses java sockets to handle get requests from a client. I am now stuck and have been looking all over google to find the answer but without success.</p>
  
  <p>Christoffers solution helped my with my first problem. Now that i have updated the code this is what i am using. </p>
  
  <p>The problem is that it only downloads parts of most webpages before it gets stuck on sending the packets back to the client loop. At the moment I cant explain why it is behaving the way it is.</p>
  
  <pre><code>public class MyProxyServer {  
      //Set the portnumber to open socket on
      public static final int portNumber = 5555;  
  
      public static void main(String[] args){  
  //create and start the proxy
  MyProxyServer myProxyServer = new MyProxyServer();  
  myProxyServer.start();  
  }  
  public void start(){  
  System.out.println("Starting MyProxyServer ...");  
  try {  
      //create the socket 
      ServerSocket serverSocket = new ServerSocket(MyProxyServer.portNumber);  
  
      while(true)
      {    
          //wait for a client to connect
          Socket clientSocket = serverSocket.accept();  
  
          //create a reader to read the instream
          BufferedReader inreader = new BufferedReader( new InputStreamReader(clientSocket.getInputStream(), "ISO-8859-1"));  
  
          //string builder for preformance when we loop over the inputstream and read lines
          StringBuilder builder = new StringBuilder();
          String host = "";
  
  
          for (String buffer; (buffer = inreader.readLine()) != null;) {
          if (buffer.isEmpty()) break;
          builder.append(buffer.replaceAll("keep-alive", "close"));
          if(buffer.contains("Host"))
              {
              //parse the host
              host = buffer.replaceAll("Host: ", "");
              }
          System.out.println(buffer);
          }
  
  
          String req = builder.toString();
          System.out.println("finshed reading \n" + req);
          System.out.println("host: " + host);
  
  
          //new socket to send the information over
          Socket s = new Socket(InetAddress.getByName(host), 80);
  
          //printwriter to send text over the output stream
          PrintWriter pw = new PrintWriter(s.getOutputStream()); 
  
          //send the request from the client
          pw.println(req+"\r\n");
          pw.flush();
  
          //create inputstream to receive the web page from the host
          BufferedInputStream in = new BufferedInputStream(s.getInputStream());
  
          //create outputstream to send the web page to the client
          BufferedOutputStream outbuffer = new BufferedOutputStream(clientSocket.getOutputStream());
  
          byte[] bytebuffer = new byte[1024];
          int bytesread;
  
          //send the response back to the client
          while((bytesread = in.read(bytebuffer)) != -1) {
          System.out.println(bytesread);
          outbuffer.write(bytebuffer,0, bytesread);
          outbuffer.flush();
          }
          System.out.println("done sending");
  
          //close the streams
          inreader.close();
          s.close();
          pw.close();
          outbuffer.close();
          in.close();
      }
  
  
  } catch (IOException e) {  
      e.printStackTrace();
  }  catch(RuntimeException e){
      e.printStackTrace();
  }
  }  
  </code></pre>
  
  <p>}</p>
  
  <p>If anyone could explain to me why i cant get it working correctly and how to solve it I would be very grateful!</p>
  
  <p>Thanks in advance.</p>