October 22, 2024
Chicago 12, Melborne City, USA
C#

Packing multiple writes into single TCP packet


I have a sequence of send() calls to write bytes to a TCP socket. Is it possible to force the socket to only send TCP packets when they are either full (exceeding the MTU) or I explicitly indicate that I’m done sequentially calling send()?

Specifically, I have some code like:

// these are user-provided, I don't control these:

void* buf1;
size_t len1;
void* buf2;
size_t len2;
void* buf3;
size_t len3;

// I control this call sequence:

if (send(sck, buf1, len1, 0) < 0 ||
    send(sck, buf2, len2, 0) < 0 ||
    send(sck, buf3, len3, 0) < 0) {
  ...
}

And I’d like to somehow minimize the number of underlying packets that are sent.

This could be done by creating an intermediate buffer then after all the send calls, I could then manually send that buffer but I’d like to avoid creating an intermediate buffer because some of the data may be quite large and it would require an extra copy of that data. In other words, I am looking to zero-copy send the data. Is this possible?

Note: I am aware that TCP is not a packet-based protocol. I’ve searched around and there are a number of answers that give that pedantic response that packets don’t really exist and while it’s perhaps semantically true, it’s not particularly helpful because there are still IP packets underneath.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video