Socket Send - Seperate Packets for Each buffer


Hiroyuki Tanaka

Dear Readers,

I am trying to learn C# network programming and I have discovered that
Windows TCPIP often joins small packets and send them as one to the
remote server. This appears to be a default behaviour of TCPIP?

I am attempting to send 1000 TCPIP packets to a remote sever with only
a sequence number (and soon a variable length message (only a few
characters)) in them.

packet 0 0
packet 1 1
packet 2 2
packet 100 100
packet 999 999

So at my remote server I wish to receive 1000 packets each with a
single number in each packet.

However at my server I get packets that include all the number to 100
(for example) joined together.

How can I force the C# socket send to send just the buffer I want in
each packet?



private void send_Click(object sender, System.EventArgs e)
int port = 5000;
Socket tcpsocket;

tcpsocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

IPEndPoint remoteServerEndPoint = new IPEndPoint(IPAddress.Parse(
"" ), port );


for (int i= 0; i < 1000 ; i++)
string count = i.ToString();
Byte[] socketbuffer = new byte[count.Length];
for (int j = 0; j < count.Length; j++)
socketbuffer[j] = System.Convert.ToByte(count[j]);

tcpsocket.Send(socketbuffer, 0, socketbuffer.Length,


Hi Tanaka,

TCP does not respect packet boundaries. Switching the Nagle algoritm off
does not garantie eather. You can use UDP, but it is very unreable, with UDP,
packet boundaries are respected, but not the order of sent, and not unique
delivery, a packet can get lost or you can receive same packet many times
(depending on routing in the network).

However I have seen in help that there is a Socket option to respect
boundaries. However you can easy do the same in your receiving application.
Eather you send first the length of the packet as leading byte/word/dword, or
work with a packet terminating character.

For the former you receive until you have gathered a byte/word/dword and see
the length, then you receive until you got length bytes. If there is more in
buffer then itis begin of next packet etc..

for the latter you just have to check the packet terminating character. But
this character may never be in the data itself. For ascii data is no problem
of course, but for binary data, you have to escape this byte.

hope this helps a little. I'm very new to NET but know a lot of winsock ;)

rgds, Wilfried


See SocketOptionName.NoDelay
"Disables the Nagle algorithm for send coalescing."

This will not guarantie that packet boundaries will be respected, becasue
winsock can concatenate the packets at receiving side as well, and if packet
is not yet transmitted at sending side (due to TCP handshaking), it is still
possible it is concatenated at sending side.

Switchin Nagle of will send faster because winsock will not wait a while for
an eventually next packet to be send. Also if it is a handshaking protocol
then Nagle has to be switched of at both sides.

rgds, Wilfried

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question