本帖最后由 qiangcai 于 2013-01-29 18:50:28 编辑

解决方案 »

  1.   

    如果仅从异常分析,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).可能需要捕捉到该异常,并进行异常恢复处理。(因为导致不同步的原因可能是不可避免的)
      

  2.   


    我在异常发生的时候进行了异常捕获,然后将发送给服务器失败的数据收回然后再重新发送。在我程序的另外一个地方有进行重新连接服务器的处理。但是现在我很想知道是什么原因导致了该异常的发生。我这里的socket是使用netty来封装的服务器。
      

  3.   

    感谢前面两位仁兄的大力支持,该问题解决了。现在我采用nio来做客户端发送数据,在linux下跑了几个小时视乎没有出现该异常了。修改了以前代码中的
    以前写入流如下
    os.write(getMsgHead(msgBytes.length));用了NIO之后写入流如下:
    ByteBuffer buffer = ByteBuffer.wrap(getMsgHead(msgBytes.length));  
    socketChannel.write(buffer);  
    buffer.clear();改了以后没有出现异常了在linux下
      

  4.   

    nio可能多了些容错处理,在稳定性上要更好吧