如果仅从异常分析,broken pipe是由服务端与客户端之间数据的不同步引起的,具体原因可能会很多看这里分析的一个例子: http://stackoverflow.com/questions/9527795/why-a-java-net-socketexception-broken-pipe-will-occurSome 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).可能需要捕捉到该异常,并进行异常恢复处理。(因为导致不同步的原因可能是不可避免的)
http://stackoverflow.com/questions/9527795/why-a-java-net-socketexception-broken-pipe-will-occurSome 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).可能需要捕捉到该异常,并进行异常恢复处理。(因为导致不同步的原因可能是不可避免的)
我在异常发生的时候进行了异常捕获,然后将发送给服务器失败的数据收回然后再重新发送。在我程序的另外一个地方有进行重新连接服务器的处理。但是现在我很想知道是什么原因导致了该异常的发生。我这里的socket是使用netty来封装的服务器。
以前写入流如下
os.write(getMsgHead(msgBytes.length));用了NIO之后写入流如下:
ByteBuffer buffer = ByteBuffer.wrap(getMsgHead(msgBytes.length));
socketChannel.write(buffer);
buffer.clear();改了以后没有出现异常了在linux下