2

I am converting the details that has to be sent from my C++ function to Java as strings and as a char* which will be sent through socket.

My buffer size is 10 MB. Can I send the 10MB in one shot or should I split and send as chunks of smaller memory?

What is the difference between those two approaches? If I should send as smaller memory what should be the chunk size?

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
Sel_va
  • 588
  • 5
  • 25
  • This is quite an opinion based thing and depends on the operating system etc. – Sami Kuhmonen Aug 06 '15 at 08:35
  • Considering that the typical [MTU](https://en.wikipedia.org/wiki/Maximum_transmission_unit) on Ethernet is 1500 bytes, the data is going to be split anyway. Also I doubt any operating systems have buffers big enough to fit all 10MB. – Some programmer dude Aug 06 '15 at 08:36
  • what does OS does in this case ?. Can u brief ur opinion – Sel_va Aug 06 '15 at 08:36
  • You could take a look at this: https://en.wikipedia.org/wiki/OSI_model – marstran Aug 06 '15 at 08:38
  • @JoachimPileborg : Do u suggest sending as a whole 10MB as the data is going to be split anyway ? – Sel_va Aug 06 '15 at 08:39
  • Please refer to: [what happens when I write data to a blocking socket, faster than the other side reads?][1] [1]: http://stackoverflow.com/questions/14241235/what-happens-when-i-write-data-to-a-blocking-socket-faster-than-the-other-side – Murad Tagirov Aug 06 '15 at 08:40
  • You can send it in one big chunk 10MB, however be prepared for "short writes", i.e. the `send()` actually sending less bytes than given; you'll have to loop in this case – chill Aug 06 '15 at 08:43
  • @chill: what is the maximum chunk size i can have ? – Sel_va Aug 06 '15 at 08:45
  • @RahulJain 2^31-1, the largest positive integer. – user207421 Aug 06 '15 at 08:46
  • @SamiKuhmonen It doesn't depend on anything more than the RFCs and the Posix specification. – user207421 Aug 06 '15 at 08:49
  • @EJP, how did you come up with the 2^31-1 limit ? – chill Aug 06 '15 at 08:50
  • @chill I've already answered that. 'The largest positive integer'. – user207421 Aug 06 '15 at 08:52
  • @EJP, the positive integers are infinite, there's no "largest"; The maximum number representable in a `size_t`, which is the parameter to `send` containing the buffer length, or in `ssize_t`, which is the return value, is platform dependent; it's quite common for these numbers to be 2^64-1 or 2^63-1, respectively. – chill Aug 06 '15 at 09:00
  • 1
    @chill Blimey what a nit-pick. I think we can leave the Peano axioms out of it while we're at it, we're talking about computers. – user207421 Aug 06 '15 at 09:07

2 Answers2

3

Can I send the 10MB in one shot

Yes.

or should I split and send as chunks of smaller memory?

No.

What is the difference between those two approaches?

The difference is that in case 1 you are letting TCP make all the decisions it is good at, with all the extra knowledge it has that you don't have, about the path MTU, the RTT, the receive window at the peer, ... whereas in case 2 you're trying to do TCP's job for it. Keeping a dog and barking yourself.

If I should send as smaller memory what should be the chunk size?

As big as possible.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    @editor If there is anything wrong with the well-known expression 'keeping a dog and barking yourself' it is news to me. Please confine your activities to genuine problems and leave my prose style alone. – user207421 Aug 06 '15 at 08:54
2

When you call the write() function, you provide a buffer and number of bytes you want to write. However it is not guaranteed that the OS will send/write all the bytes that you are willing to write in a single shot. (In case of blocking sockets, the write() call would block until it copies the entire chunk to the TCP buffer. However in case of non-blocking ones, the write() would return and would not block and would write the just the bytes it is able to).

The TCP/IP stack runs in the OS and each OS will have its own implemenation of the stack. This stack would determine the buffer sizes and moreover the TCP/IP would itself take care of all the low level statistics such as MSS, the available receiver window size, which would let TCP run the flow control, congestion control related algorithms.

Therefore it is best that let TCP decide how would it want to send your data. Instead of you breaking the data into chunks, let the TCP stack do it for you.

Just be careful with the thing that always check the number of bytes actually sent which is returned by the write() call.

Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42
  • It *is* guaranteed, in blocking mode, by the Posix specification, except in the rare case of an interrupted system call. – user207421 Aug 06 '15 at 09:03
  • Yes.. in non-blocking sockets number of bytes written may not be equal to the bytes we want to write. However it is always good to have a check whichever type of sockets we may use. – Sumit Trehan Aug 06 '15 at 09:10