Asher
Asher

Reputation: 1026

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?

Upvotes: 0

Views: 163

Answers (2)

ChrisBD
ChrisBD

Reputation: 9209

The short answer is no.

If using the Socket class you'll need to implement receive and transmit buffers along with a thread for sending and one for reception, if transmitting asynchronously.

You sould define your message to have either a start and end character sequence or a start sequence with a known message size.

It's natural for TCP to split messages, but the data will always arrive in the correct order.

Upvotes: 1

C.Evenhuis
C.Evenhuis

Reputation: 26456

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.

Upvotes: 2

Related Questions