Threading in client-server socket program - proxy sever

Posted by crazyTechie on Stack Overflow See other posts from Stack Overflow or by crazyTechie
Published on 2012-10-28T04:34:59Z Indexed on 2012/10/28 5:00 UTC
Read the original article Hit count: 166

Filed under:
|
|

I am trying to write a program that acts as a proxy server. Proxy server basically listens to a given port (7575) and sends the request to the server. As of now, I did not implement caching the response.

The code looks like

ServerSocket socket = new ServerSocket(7575);   
Socket clientSocket = socket.accept(); 
clientRequestHandler(clientSocket);

I changed the above code as below: //calling the same clientRequestHandler method from inside another method.

     Socket clientSocket = socket.accept();
     Thread serverThread = new Thread(new ConnectionHandler(client));
     serverThread.start();



    class ConnectionHandler implements Runnable {
Socket clientSocket = null;
ConnectionHandler(Socket client){
    this.clientSocket = client;
}
@Override
    public void run () {

    try {
        PrxyServer.clientRequestHandler(clientSocket);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    }
   }

Using the code, I am able to open a webpage like google. However, if I open another web page even I completely receive the first response, I get connection reset by peer expection.

1. How can I handle this issue Can i use threading to handle different requests. Can someone give a reference where I look for example code that implements threading.

Thanks.

Thanks.

© Stack Overflow or respective owner

Related posts about http

Related posts about sockets