开发者

Simple Java HTTP-Proxy with Sockets gets stuck without error message

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...)).

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(">>>>" + 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();
        }
}



}

(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.

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.

Any help is appreciated, thanks in advance!


You need two threads: one to read from the client and write to the server, and one to do the opposite, for each accepted socket. There is a further subtlety: when you read EOS from one direction, shutdown the opposite socket for output, and then if the input socket for that thread is already shutdown for output, close both sockets. In both cases exit the thread that read the EOS.


Try getting first the OutputStream and then the InputStream!

InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();

change it to:

OutputStream clientOut = client.getOutputStream();
InputStream clientIn = client.getInputStream();


This will make it work:

It will check if there is more data available to read

Still, it's important that you use BufferedIS because I think ByteArrayIS doesn't implement available method.

 BufferedInputStream bis = new BufferedInputStream(clientdata);
            System.out.println("beforetry");

            try {
                while(bis.available() > 0){
                    n = bis.read(buffer);
                    response.write(buffer, 0, n);
                }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜