Search code examples
c#sockets.net-2.0

Socket class breaks text on send


I am using the Socket class to send a text message to a remote server via the Send() method.

The text I am sending is has no breaks:

"The|string|that|is|being|sent|"

but the remote server receives it as

"The|st"
""
"ring|t"
""
"hat|is"
""
"|being"
""
"|sent|"

Is there a setting on the Socket class that can prevent this?


Solution

  • This is normal behavior for TCP (which I assume you're using) - you have no real control on how packets get fragmented, it only guarantees that packets are delivered in the same order.

    Usually applications send a termination character (a newline character, for instance) to indicate one message was sent, and new data belongs to the next message.

    I wonder where you get those empty strings from - as a message of 0 bytes indicates the other side wants to close the connection.