Java socket output stream and broken pipe

I need to send the size of a dynamic buffer to a socket stream. It works correctly, but when I try to send multiple buffers larger than int my_buffer_size =18 * 1024

; (this is a guideline value)

I am getting error (for some writing):

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 large file, I read the 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 whose buffer size can be variable)

SendBufferSize - 146988.

Is there a way to fix the broken pipeline error? I read, but I didn't actually solve the problem.

Thank. any help is appreciated

I am using the classic ServerSocket serverSocket; and Socket cclient

+3


source to share


1 answer


Broken pipe means you wrote data to a connection that was already closed by the other end.

Ergo the problem lies at the other end, not in this code. Maybe the other end doesn't really understand your long word protocol, for example, or doesn't implement it correctly.



If it is something like this the code won't be because you are ignoring the result being returned read()

and assuming it fills the buffer. This is not specified, only to carry at least one byte.

+4


source







All Articles