java.net.SocketException:peer重置连接:为文件提供服务时发生套接字写入错误
我正在尝试使用套接字实现HTTP服务器。如果客户端(例如浏览器)请求目录,服务器将显示可用文件的列表。当客户端请求文件时会出现问题。我得到以下错误:java.net.SocketException:peer重置连接:为文件提供服务时发生套接字写入错误,java,sockets,socketexception,Java,Sockets,Socketexception,我正在尝试使用套接字实现HTTP服务器。如果客户端(例如浏览器)请求目录,服务器将显示可用文件的列表。当客户端请求文件时会出现问题。我得到以下错误: java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(So
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at cf.charly1811.java.web.RequestHandler.writeFile(RequestHandler.java:152)
at cf.charly1811.java.web.RequestHandler.processRequest(RequestHandler.java:139)
at cf.charly1811.java.web.RequestHandler.handleRequest(RequestHandler.java:110)
at cf.charly1811.java.web.RequestHandler.run(RequestHandler.java:86)
at java.lang.Thread.run(Thread.java:745)
stacktrace显示问题来自writeFile()
方法:
private void writeFile(File request) throws IOException
{
InputStream byteReader = new BufferedInputStream(new FileInputStream(request));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = byteReader.read(buffer)) != -1)
{
outputStream.write(buffer, 0, bytesRead);
}
byteReader.close();
}
但我不知道怎么了。
你能帮我吗
编辑
谢谢大家的回答。在我阅读了你的答案后,我了解到问题是发生错误时,套接字被损坏了。这是我的错误代码:
// Method to process a single request
handleRequest() throw IOException
{
// process here
// if the client request a file
writeFile();
// close socket when the request is processed
}
// The method is called
public run()
{
try{
// If an error occurs the try/catch won't be called because it is implemented outside the loop. So when an IOException occurs, the loop just stop and exit the program
while(true)
{
handleRequest();
}
}
catch(IOException e) {
// Handle exception here
}
}
我的新代码是这样的:
// Method to process a single request
handleRequest()
{
try {
// process here
// if the client request a file
writeFile();
// close socket when the request is processed
}
// If this exception occurs the catch() method will be called
catch(IOException e)
{
// handle exception here
}
}
// The method is called
public run()
{
while(true)
{
handleRequest();
}
}
}
此问题通常是由于写入已被对等方关闭的连接而导致的。在这种情况下,它可能表示用户取消了下载。例如,TCP套接字可能正在“关闭”,而您的代码尚未收到通知 这是一个生命周期的动画 基本上,连接是由客户端关闭的。您已经有了
抛出IOException
和SocketException
扩展IOException
。这很好用。您只需要正确处理IOException
,因为它是api的正常部分
编辑:当在不存在或已关闭的套接字上接收到数据包时,
RST
数据包出现。你的申请没有什么不同。根据实施情况,重置
状态可能会持续,并且关闭
将永远不会正式出现。我面临这个问题,但解决方法非常简单。
我正在1024字节缓冲区中写入1MB文件,导致此问题。
要理解,请参阅修复前后的代码
带有例外项的代码
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
while (fis.read(buffer) > 0) {
dos.write(buffer);
}
修复后:
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[102400];
while (fis.read(buffer) > 0) {
dos.write(buffer);
}
您可能需要查看远程主机(“clent”)上发生的情况。建议:试试。当IOException发生时,您的
while(true)
循环必须正常退出。编辑中的新代码出现了一个新问题,即如果套接字异常关闭,while
循环将永远旋转。@baconoverlord对此有何建议?@Charly1811是handleRequest
与writeFile
的同义词?很可能我没有完全理解您正在显示的内容中缺少的元素。writefile()在HandlerRequest()中调用,我忘记在回答中通知它。抱歉@baconoverlord端口可能正在关闭,但该状态与此问题无关。这意味着两端同时关闭,在这种情况下,此写入操作将引发“套接字关闭”异常。它表示由对等方重置:套接字写入错误
<当目的地接收到闭合套接字的数据包时,会发生代码>RST重置数据包。两端中的一个以异常方式终止了连接,或者它发生得太快(可能是在环回上),以至于在关闭过程中发生。我经常看到这个错误,知道write()
上的所有IOException都应该像关闭连接一样进行处理。@EJP这与问题有关。试图进一步解释生命周期,而不是回答“已被对等方关闭的连接”。错误本身表示“由对等方重置”。你不应该否决那些每天都在使用插座的人,他们已经在生产中的每个主要操作系统上看到了各种可能的中间状态。你是对的@baconoverlord。发生IOException时,未调用catch()方法。正常情况下,不应关闭套接字并停止程序。我用fix@bond链接不起作用。你能修好它吗?谢谢