Connection reset by peer: socket write error

Rona picture Rona · Jan 20, 2012 · Viewed 10.7k times · Source

I am getting an exception on OutputStream.write in the last while loop (it works fine on other places in the code) when running this code- this is an implantation of a proxy-server in java, when searching in the host-response for content length and forwarding the result to the browser it works, but when trying to handle "Transfer-Encoding: chunked" policy the same method does not work' and throws this exception:

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

Answer

Rolf Rander picture Rolf Rander · Jan 20, 2012

I might have misread your code, but it seems that you write all headers (including "Transfer-Encoding: chunked") to os, but you don't write the actual chunk sizes, so the receiving end probably closes the connection because of illegal input (expecting the chunk size, get other data)