0

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
    }
}
SC Bose
  • 57
  • 1
  • 6
  • The connection is already closed (reset) when you are trying to read :) It is hard to know why when I dont have the full code though. – Pphoenix Jun 12 '14 at 08:56
  • I do assume that yuo have the same issue as http://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset – Pphoenix Jun 12 '14 at 08:58
  • @Pphoenix Do you think I can reopen it somehow every time the loop srats its iteration by reinitialising it in the loop? – SC Bose Jun 12 '14 at 09:02
  • It was too long for a comment so I added as answer instead – Pphoenix Jun 12 '14 at 09:09
  • Why are you performing exactly one read, ignoring both the read count and the data read, and then forwarding the request, and then blocking on accepting another connection, and without closing the previous one? – user207421 Jun 12 '14 at 09:47
  • @EJP Actually I am not ignoring them, I am extracting the host address in a string and other string manipulation in this method but they are not affecting the inputstream. As to closing it, I am not sure where I should close them? Can you show me what you are suggesting in short? Since it is in a loop, will it open the connection every time? What will be the effect of just leavinng them open for the life of the loop? – SC Bose Jun 12 '14 at 11:05
  • @SCBose: Could you provide the client aswell? I guess that the problem lies there. – Pphoenix Jun 12 '14 at 11:11

1 Answers1

0

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.

Community
  • 1
  • 1
Pphoenix
  • 1,333
  • 13
  • 32