连接被对等方重置:套接字写入错误
运行此代码时,我在最后一个 while 循环中的 OutputStream.write 上遇到异常(它在代码中的其他地方工作正常),这是在主机响应中搜索时在 java 中植入代理服务器内容长度并将结果转发到浏览器它可以工作,但是当尝试处理“传输编码:分块”策略时,相同的方法不起作用'并抛出此异常:
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--;
}
}
}
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--;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能误读了你的代码,但似乎你将所有标头(包括“Transfer-Encoding:chunked”)写入
os
,但你没有写入实际的块大小,因此接收端可能由于非法输入而关闭连接(期望块大小,获取其他数据)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)