为什么"java.net.SocketException:管道损坏"?会发生? [英] Why a "java.net.SocketException: Broken pipe" will occur?

查看:55
本文介绍了为什么"java.net.SocketException:管道损坏"?会发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的套接字程序,它工作正常,但是我的朋友使用端口扫描工具,当它扫描到我正在使用的端口时,它会显示"java.net.SocketException:管道破裂"错误,这是怎么回事?以及如何解决?

I wrote a simple socket programme, it works fine, but my friend use a port scanning tool, when it scan to the port I am using, it cash with "java.net.SocketException: Broken pipe" error, what's happen? and how to solve it?

providerSocket = new ServerSocket(portNum);
connection = providerSocket.accept();
if (connection.getOutputStream() != null) {
    //this line crash!!!
    out = new ObjectOutputStream(connection.getOutputStream());
    out.flush();

}

控制台的详细信息错误:

Detail Error from console:

java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1847)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1756)
    at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:230)

推荐答案

某些端口扫描程序通过开始打开连接然后立即终止连接来工作.您的服务器未编程为处理连接失败,因为您未针对这种可能性进行编码.您将需要使用try/catch来捕获该状况并进行恢复.另外,您可能应该将连接移交给单独的线程进行处理,以便您的主程序可以继续接收新的连接(并将它们发送给要处理的线程).

Some port scanners work by starting to open a connection and then immediately terminating it. Your server is not programmed to deal with a connection failure because you did not code for that possibility. You will need to use a try/catch to trap that condition and recover. Also, you should probably be handing off the connection to a separate thread for processing, so your main program can continue to receive new connections (and sending them to threads to be handled).

这篇关于为什么"java.net.SocketException:管道损坏"?会发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆