简体   繁体   English

TCP套接字上的管道损坏读取

[英]Broken pipe on TCP socket read

I have an application in Java that opens a TCP server socket and then reads and writes from this socket. 我有一个Java应用程序,该应用程序打开一个TCP服务器套接字,然后从该套接字读取和写入。 I'm running into a problem where the server is getting a IOException with the message "Broken pipe" on a read . 我遇到了一个问题,即服务器在读取时获得IOException消息“ Broken pipe”。 I have a good understanding of why this would happen on a write, but I can't figure out why it would happen on a read. 我对为什么在写入时会发生这种情况有很好的了解,但是我不知道为什么在读取时会发生这种情况。 For reference, this is the top part of the stack trace 供参考,这是堆栈跟踪的顶部

Caused by: java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:21)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:198)
at sun.nio.ch.IOUtil.read(IOUtil.java:166)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:243)

I'm assuming what must be happening is that the OS giving an EPIPE error, but I can't find the code where that would happen. 我假设必须发生的事情是操作系统出现EPIPE错误,但是我找不到发生此错误的代码。 I've looked through the JDK source code (Java and C source) for the text 'Broken pipe' and I can't find the piece of code that is actually generating this exception. 我已经在JDK源代码(Java和C源代码)中查找了文本“断管”,但找不到真正产生此异常的代码。

Ultimately the question I'm trying to answer is why this is happening, but I would also accept an answer that simply explains where this error is even generated. 最终,我要回答的问题是为什么会发生这种情况,但是我也将接受一个答案,该答案仅说明了在何处生成此错误。

It happens on a read for the same reason it happens on a write. 读取时发生的原因与写入时发生的原因相同。 You wrote to a connection that had already been closed by the peer. 您写了一个已被同级关闭的连接。 You got it on the read because the exception was deferred due to buffering. 您可以读取它,因为由于缓冲而推迟了异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM