4

I have to send a dynamic buffer size to the socket stream. It works correctly, but when I try to send multiple buffers with a size bigger than int my_buffer_size =18 * 1024; (this is an indicative value)

I get the error (for some write):

Java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)

My code is very simple: For example If I want to send a big file I read a file stream with

byte[] bs = new byte[my_buffer_size];
while (... ){ 
fileInputStream.read(bs);
byte[] myBufferToSend = new byte[sizeBuffer];
DataOutputStream out = new DataOutputStream(cclient.getoutputStream());
out.writeInt(myBufferToSend.length);
out.write(myBufferToSend);
out.flush();
}

(The file is just a test the buffer size can be variable)

the SendBufferSize is 146988.

Is there a way to fix the broken pipe error? I read around but actually I didn’t solve the problem.

Thank you any help is appreciated

I use the classic ServerSocket serverSocket; and Socket cclient

4

1 回答 1

4

'Broken pipe' 表示您已将数据写入另一端已关闭的连接。

因此问题出在另一端,而不是在这段代码中。例如,另一端可能并不真正理解您的长度字协议,或者没有正确实现它。

如果它与此代码类似,则不会,因为您忽略了返回的结果read()并假设它填充了缓冲区。没有指定这样做,只传输至少一个字节。

于 2015-01-05T23:34:24.760 回答