Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Java HTTP-Proxy with Sockets gets stuck without error message
    text
    copied!<p>I'm trying to get a simple multithreaded proxy to work in Java. However I don't manage to get the webpage show up in my browser, after the first GET request and the response from the webpage, the program is just stuck (as you can see from my code, I'm printing everything i get on the stdout for debugging, and there I see the sourcecode of the webpage, however after printing out "After Client Write", nothing happens (no exception, just nothing...)).</p> <pre><code>import java.net.*; import java.util.*; import java.io.*; public class Proxy { public static void main(String[] args) { try { ServerSocket listensocket = new ServerSocket(Integer.valueOf(args[0])); while(true) { System.out.println("wait"); Socket acceptsocket = listensocket.accept(); // blocking call until it receives a connection myThread thr = new myThread(acceptsocket); thr.start(); } } catch(IOException e) { System.err.println("&gt;&gt;&gt;&gt;" + e.getMessage() ); e.printStackTrace(); } } static class myThread extends Thread { Socket acceptsocket; int host, port; String url; myThread(Socket acceptsocket) { this.acceptsocket=acceptsocket; } public void run() { try { System.out.println("hello"); Socket client = acceptsocket; //client.setSoTimeout(100); InputStream clientIn = client.getInputStream(); //BufferedInputStream clientIn=new BufferedInputStream(clientis); OutputStream clientOut = client.getOutputStream(); System.out.println("hello"); String clientRequest = readStuffFromClient(clientIn); // parses the host and what else you need System.out.print("Client request: -----\n"+clientRequest); Socket server; server = new Socket("xxxxxxxxxxxxx" , 80); InputStream serverIn = server.getInputStream(); //BufferedInputStream serverIn=new BufferedInputStream(serveris); OutputStream serverOut = server.getOutputStream(); serverOut.write(clientRequest.getBytes()); serverOut.flush(); String serverResponse = readStuffFromClient(serverIn); System.out.print("Server Response: -----\n"+serverResponse); clientOut.write(serverResponse.getBytes()); clientOut.flush(); System.out.println("After Client Write"); clientIn.close(); clientOut.close(); serverIn.close(); serverOut.close(); server.close(); client.close(); } catch(Exception e) { System.out.println(e); } } private String readStuffFromClient(InputStream clientdata) { ByteArrayOutputStream response = new ByteArrayOutputStream(); StringBuffer request=new StringBuffer(8192); int i, httpstart,n=-1 ; byte[] buffer = new byte[8192]; System.out.println("beforetry"); try { while((n = clientdata.read(buffer))!=-1) { System.out.println("before"); response.write(buffer,0,n); //response.flush(); } request=new StringBuffer(response.toString()); /*System.out.println("new:"+n+" "+ request.toString()); System.out.println("End client data");*/ } catch (Exception e) { System.out.println("here"); System.out.println(e); e.printStackTrace(); i = -1; } System.out.println("End manipulation method"); return request.toString(); } } } </code></pre> <p>(this is a stripped down not working example of my program, from the comments one can see I already tried to use BufferedInputStream). In general, this program is very unresponsive even for the first GET request from the browser. When I only read the clientdata once (not in a loop), I get a little bit further, e.g. get more GET/Response pairs, but at some point the program still gets stuck.</p> <p>Somehow I think either I've a real trivial error I just don't manage to see, or the program should work, but simply doesn't for no real reason.</p> <p>Any help is appreciated, thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload