2

I'd have a question regarding java SocketChannel.

Say I have a socket channel opened in blocking mode; after calling the the write(ByteBuffer) method, I get an integer describing how many bytes were written. The javadoc says: "Returns: The number of bytes written, possibly zero"

But what exactly does this mean? does this mean that the number of bytes really has been delivered to the client (so that sender received tcp ack making evident how many bytes have been received by server), or does this mean that the number of bytes has been written to the tcp stack? (so that some bytes still might be waiting e.g. in the network card buffer).

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

2 Answers2

5

does this mean that the number of bytes really has been delivered to the client

No. It simply means the number of bytes delivered to the local network stack.

The only way to be sure that data has been delivered to the remote application is if you receive an application level acknowledgment for the data.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thanks for your reply. I guess I should study how berkley sockets are implemented in various OS, to understand this precisely – johny_walker Sep 13 '10 at 13:57
  • @Stephen C Does SocketChannel provide reliablity that all bytes written would surely be delivered to remote SocketChannel? – rns Aug 09 '14 at 09:06
  • 1
    @Soni - No it doesn't. If there is a network outage, the bytes send may never be received. Indeed, there is a small probability that data may be corrupted in transit and the corruption isn't detected. – Stephen C Aug 09 '14 at 12:24
  • Could you provide sources for your answer, please? – mike Feb 10 '15 at 16:14
  • @mike - I don't have one. This is "common knowledge". The kind of thing I learned 30+ years ago when I first learned about TCP/IP, sockets and so on. However, if >>you<< find decent sources, please let me know and I will add them to the answer. – Stephen C Feb 10 '15 at 21:31
0

The paragraph that confuses you is for non-blocking I/O.

For non-blocking operation, your initial call may indeed not write anything at the time of the call.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer.

As you can see, for blocking I/O all bytes would be sent ( or exception thrown in the middle of the send )

Note, that there is no guarantee on when bytes will appear on the receiving side, this is totally up to the low level socket protocol.

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • I understand the blocking/non-blocking issue, my question rather was if (after the bytes were written) I can rely on the fact that requested bytes were delivered to the remote side or just to the local tcp/ip stack. Anyway thanks for comment – johny_walker Sep 13 '10 at 14:12