Abruzzo Forte e Gentile
Abruzzo Forte e Gentile

Reputation: 14879

writing data to a socket that is sent in 2 frames

My appliactions sends through the wire using socket small messages. Each message is around 200 bytes of data. I would like to see my data sent in 2 frames instead of 1. My questions are

  1. How to do that i.e. is there a way to cause TCP to automatically split the buffer in 2 frames?
  2. Do I get the same if I send my buffer in 2 separate writes?

I am using Linux and C.

Upvotes: 3

Views: 1078

Answers (2)

Ziffusion
Ziffusion

Reputation: 8943

Here are a few things you can consider.

  1. TCP does have the PSH flag, that you can set in a packet, that makes TCP push out any buffered data. But this will work somewhat unreliably, because, in theory, data can get combined again on the receiving side. But in practice, you will see the data being delivered separately.
  2. You can't really use "\n" as a delimiter, because it can occur naturally in your data. You have to come up with some kind of a escape sequence to use, and escape all the occurrences of "\n" in the data. This can be painful.
  3. If you need message boundaries, consider a protocol that supports it. Like UDP. But with UDP you lose guaranteed delivery. You will have to roll your own confirmations, retries and what not.
  4. Finally there is SCTP. Less used protocol, but available in the Linux stack at least. It gives you best of both worlds. Message boundaries, guaranteed delivery, guaranteed sequence.

Upvotes: 0

masoud
masoud

Reputation: 56549

How to do that i.e. is there a way to cause TCP to automatically split the buffer in 2 frames?

TCP is a stream communication protocol, all data is continuous. You should split your data by delimiters.

For example, in HTTP protocol each separated request is splited by two \n.

Do I get the same if I send my buffer in 2 separate writes?

No, you will receive them as a one continuous data stream. Frames are meaningless.

Note: Before you receive any data TCP in your application, packets are separated but OS collect and reassemble them. This process is transparent from your application.

Upvotes: 5

Related Questions