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
}
}