5

I was reading this thread and noticed in the solution that if you send the length of the data separate from the actual data Nagle's algorithm may come in to play and desync the data sent.

I was wondering about this because a lot of code samples I see the client sends the length of the data first and then the data itself as to separate calls. Was this problem not affecting them somehow?

Should I concatenate the length of the data and the data itself in to a single byte array and send that in one go? If so, is there a better way to concatenate the two byte arrays without manually copying all the data from both (loops) in to the new one? Seems pretty inefficient for something that would have to occur so often (every time the user or server sends data between each other).

Community
  • 1
  • 1
John Smith
  • 8,567
  • 13
  • 51
  • 74

1 Answers1

2

If you've created a Socket with the NoDelay option set, then you won't be affected by the Nagle algorithm; data passed to Send will be immediately sent.

Given this, it would be more efficient to pass a single byte array in a single Send call, rather than separate calls; each call to Send will have the overhead of a packet header.

Blair Holloway
  • 15,969
  • 2
  • 29
  • 28