简体   繁体   English

Java中的代理:读取输入流时出现套接字异常

[英]Proxy in Java : Socket Exception while reading an inputstream

I am implementing a simple proxy server in Java. 我正在用Java实现一个简单的代理服务器。 I read in the requests from the browser through a clientSocket in the following code snippet. 我在以下代码片段中通过clientSocket从浏览器中读取了请求。 Then I open an input stream and read in the responses through byte[] buffer. 然后,我打开一个输入流,并通过byte []缓冲区读取响应。

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]; 终止点是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. 其他信息:我正在SwingWorker对象中运行此方法(可能与它无关),forwardRequest也转发被拦截的请求。

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? 是什么导致我的java.net.SocketException:连接重置? I found the difference between connection reset and connection reset by peer . 我发现了connection resetconnection reset by peer之间的区别。

The connection reset message means that you abruptly closed the connection (and by peer means that the other party closed it). connection reset消息表示您突然关闭了连接(对等体意味着另一方关闭了该连接)。 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. 这不是100%的绝对答案,但是由于我无法测试您的所有代码,因此值得尝试。 If you know that you are testing with multiple clients, I would definitely suggest you try this solution. 如果您知道要与多个客户端进行测试,则绝对建议您尝试此解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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