Blocking mode NIO socket channel does not write all data, what to do?
897898Nov 3 2011 — edited Nov 4 2011Hi, I am writing to a socket channel, which is in blocking mode:
int bytesWritten = channel.write(buffer);
int bufferRemaining = buffer.remaining();
I would expect the call to write() to block until all data is written, so bytesWritten always = remaining() before the call, and remaining() after the call always = zero.
I am writing fast, and the receiver is backing up the TCP connection, until the local send buffer is full. When this happens, the call to channel.write() does not block until space is available, as I expected it would. bufferRemaining is > 0, so what do I do?
Simply call channel.write() in a loop until all the data goes? Won't that be polling and incur a high CPU cost when it happens?
This is under Java 6_20, on Debian, running Linux kernel 2.6.32-5-amd64. Generally, I've found NIO can work quite differently under Windows, so this problem may be Linux specific. Maybe I should file it as a bug report, or try Java 7 and see if that is any better.
Thanks for your help.
Rupert