简体   繁体   中英

Proxy in Java : Socket Exception while reading an inputstream

I am implementing a simple proxy server in Java. I read in the requests from the browser through a clientSocket in the following code snippet. Then I open an input stream and read in the responses through byte[] buffer.

I am encountering an error while reading in a particular webpage running on my local server. The particular point of termination is the line int n = bis.read[buffer]; Could someone point out what is causing it and how to rectify it?

Other Info: I am running this method in a SwingWorker object (Maybe its not relevant) Also the forwardRequest forwards the intercepted request.

If you require anything else, shoot me an update or a comment

The Stack Trace:

ERROR
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.net.SocketInputStream.read(SocketInputStream.java:107)
at Networker.doInBackground(Networker.java:67)
at Networker.doInBackground(Networker.java:25)
at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at javax.swing.SwingWorker.run(SwingWorker.java:335)


@Override
protected Void doInBackground() throws Exception {
    System.out.println("Starting the SimpleProxyServer ...");

    while (true) {
        clientSocket = serverSocket.accept();
        InputStream bis = clientSocket.getInputStream();

        // reading the request and put it into buffer
        //BufferedReader br =new BufferedReader(new InputStreamReader(bis));
        try {
            n = bis.read(buffer);
        } catch (Exception e) {
            System.err.println("ERROR");
            e.printStackTrace();
        }
        forwardReq(); //this forwards the intercepted request
    }
}

I re-read your question now, and I think I might have found your problem.

From What's causing my java.net.SocketException: Connection reset? I found the difference between connection reset and connection reset by peer .

The connection reset message means that you abruptly closed the connection (and by peer means that the other party closed it). Since the exception is thrown at the server, the problem lies somewhere within your server code.

I believe that your unclosed streams might be a problem. There is only a finite number of resources for your program to use. When you open a stream and don't close it, the program will think that you are still using the stream and allocated resources will not be freed. Thus, testing with multiple clients may allocate all of the server's resources, and when a new client connects one of the inputstreams will be closed by force (since no resources are free). This might lead to a connection by force , resulting in a reset connection exception.

This is not a 100% absolute answer, but since I cannot test all of your code it is an idea worth trying. If you know that you are testing with multiple clients, I would definitely suggest you try this solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM