通过对等方重置连接:套接字写入错误

时间:2012-01-20 16:43:47

标签: java socket.io proxy-server

我在最后一个while循环中的OutputStream.write上得到一个异常(它在代码中的其他地方工作正常)运行这段代码时 - 这是java中代理服务器的植入,当在主机中搜索时 - 响应内容长度并将结果转发给它可以工作的浏览器,但是当尝试处理“Transfer-Encoding:chunked”策略时,相同的方法不起作用'并抛出此异常:

 java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at IncomingRequest.run(IncomingRequest.java:250)

 is = hostSocket.getInputStream();
            OutputStream os = client.getOutputStream();

            System.out.println("Forwarding request from server");

            byte[] currByte = new byte[1];
            StringBuilder responseHeader = new StringBuilder();
            int crlfCounter = 4;

            // Separates the response header by looking for 2 consecutive \r\n

            while (crlfCounter > 0){
                is.read(currByte);
                os.write(currByte);
                System.out.print((char)currByte[0]);
                responseHeader.append((char)currByte[0]);
                if ((char)currByte[0] == '\r' || (char)currByte[0] == '\n'){
                    crlfCounter--;
                }else{
                    crlfCounter = 4;
                }
            }

            StringBuilder chuckSize = new StringBuilder();
            int contentLength = 0;
            int contentIndex = responseHeader.toString().indexOf("Content-Length: ");
            int chunkedIndex = responseHeader.toString().indexOf("Transfer-Encoding: chunked");
            if (contentIndex != -1) {
                contentIndex += 16;
                int conEnd = responseHeader.toString().indexOf('\n', contentIndex);
                contentLength = Integer.parseInt(responseHeader.toString().substring(contentIndex,conEnd).trim());
                System.out.println("Content Length is : " + contentLength);
                while (contentLength > 0){
                    is.read(currByte);
                    os.write(currByte);
                    contentLength--;
                }
                os.write('\r');
                os.write('\n');
                os.write('\r');
                os.write('\n');
            } else  if (chunkedIndex != -1){
                boolean lastChunk = false;
                while (!lastChunk) {

                    do {
                        is.read(currByte);
                        chuckSize.append((char) currByte[0]);
                    } while ((char)currByte[0] != '\n');

                    contentLength = Integer.parseInt(chuckSize.toString().trim(), 16);
                    System.out.println("Hex " + chuckSize.toString());
                    System.out.println(contentLength + "     the number");

                    if (contentLength == 0) {
                        lastChunk = true;
                    }

                    while (contentLength > 0){
                        is.read(currByte);
                        os.write(currByte);
                        contentLength--;
                    }
                }
            }

1 个答案:

答案 0 :(得分:2)

我可能误读了您的代码,但似乎您将所有标头(包括“Transfer-Encoding:chunked”)写入os,但您没有写出实际的块大小,因此接收端可能因非法输入而关闭连接(期望块大小,获取其他数据)