• Post Reply Bookmark Topic Watch Topic
  • New Topic

writing to socket (and disconnecting)

 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing data to a socket using a BufferedOutputStream. I am also incrementing a progress bar with each loop. If I pull the network cable the progress bar continues to update for some seconds. It appears that data is still being written from the buffer to the socket for a few seconds before the now disconnected socket is detected? Is there a quick way to detect the socket state.

Thanks
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fact that you are using BufferedOutputStream, buffers what you write to some size (Buffer size) and when the buffer is full then ties to flush to the underlying socket.

If you want to detect the socket failure immediately, either use unbuffered output stream or set buffer size as zero.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response.

I am flushing the stream with each write.
bos.write (buf,offset,size);
bos.flush();


The delay is not crucial. Are there are any reasonable steps I could take to ensure an early detection. I will test performance on an unbuffered stream, but I suspect it will not be ideal. Also can you elaborate on your statement "set buffer size as zero".

Thanks
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If every write is followed by a flush, you might as well use the socket's OutputStream directly; flush does nothing there. I'm not sure how you would set a BufferedOutputStream's size to 0, since the constructor will throw an IllegalArgumentException if you try.

In addition to Java's buffers, TCP sockets have their own buffers at the operating system level; see setSendBufferSize(int). This may be what you're seeing. You can try making this smaller, but it's only a hint, and having a large TCP buffer is essential to getting good throughput on anything slower than a LAN.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you for taking the time to respond.

If I use flush after every write I am negating the benefits of a BufferedOutputStream. This appears obvious now that you have mentioned it.

Setting bFlush to true only on the last write for each file:
bos.write (buf,offset,size);
if(bFlush) bos.flush();

The setSendBufferSize(int) is the same size as my buffer (8192).

Thanks
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The send buffer is maintained by the OS, so that's (at least) 8 KB of data that you can flush to the socket without blocking after the network gets disconnected. Modern operating systems will vary the size of the send buffer according to response times, etc., so it could be even more.

I can't think of any way to check this buffer from Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!