Java NIO: Sending large messages quickly leads to truncated packets and data loss?

There are many reasons why sc.write() would not send some or all of the data. You have to check the return value and/or the number of bytes remaining in the buffer.

There are many reasons why sc.write() would not send some or all of the data. You have to check the return value and/or the number of bytes remaining in the buffer. For (int n=0; n 0 && bb.remaining() == 0) { // all data sent } else { // could not send all data.

} bb.rewind(); }.

You are not checking the return value of: sc. Write(bb); This returns the number of bytes written, which might be less than the data available in your buffer. Because of how nio works you can probably just call remaining() on your bytebuffer to see if there are any left.

Yep! It's not reasonable to expect to be able to send data over the network as fast as you can spew it. If you're not blocking, you need to test the results of your writes.

– erickson Jan 31 '09 at 17:04.

I haven't done any NIO programming, but according to the Javadocs, sc.write() will not write the entire ByteBuffer if the SocketChannel is in non-blocking mode (as yours is) and the socket's output buffer is full. Because you are writing so quickly, it is very likely that you are flooding your connection and your network or receiver cannot keep up. I'd be happy if I could programmatically check if it is not ready for more data yet You need to check the return value of sc.write() to find out whether your output buffer is full.

Don't assume you have any control over what data ends up in which packet. More here: stackoverflow.com/questions/453609/whats....

It seems that "sc.socket(). SetTcpNoDelay(true);" will help to ensure that multiple messages are not combined into single packets. – ZenBlender Jan 31 '09 at 18:33 I'm not entirely familiar with that method call but help!

= guarantee which means the technique I've explained in the link above is more prudent anyway. – Spencer Ruport Feb 1 '09 at 6:14.

You are using the non-blocking mode: sc. ConfigureBlocking(false); Set blocking to true and your code should work as it is. Suggestions made by others here to check send count and loop will also work.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions