1752328 Members
5999 Online
108786 Solutions
New Discussion юеВ

sockets

 
SOLVED
Go to solution
Kapil_2
Advisor

sockets


When a TCP write is done on a socket, does the data get written to the TCP buffer in an atomic manner?
For eg. if 10K is written on the socket using a single write, can the receive call return less than 10K (say 8K in the first call and 2K in the nect call) ?

Kapil
7 REPLIES 7
harry d brown jr
Honored Contributor

Re: sockets


When you read from a socket, you may specify the byte count to read.
Live Free or Die
rick jones
Honored Contributor
Solution

Re: sockets

i'm not sure that "atomic" is the right term, but no, TCP does not do atomic writes as you describe - what we would otherwise call "preserving message boundaries"

TCP is a byte-stream protocol. You feed it a stream of bytes (in whatever size chunks you choose) and a stream of bytes arrives at the other end.

A send() of 10K of data may result in several 1460 byte recv()'s or some other value <= 10K.

Your application has to be prepared to do things to preserve message boundaries. Typically, this is done by pre-pending a "header" with the total message size in it.

I would suggest you write this "header" and the data to the socket at the same time - with say a call to writev() or sendmsg(). Do not do separate writes or you will encounter the nagle algorithm.

the works of W. Richard Stevens would be very good to have handy if you are going to do any "network programming"
there is no rest for the wicked yet the virtuous have no pillows
A. Clay Stephenson
Acclaimed Contributor

Re: sockets

Hi:

I suggest you look at the sendmsg() and recvmsg() system calls. These will come closest to doing what I think you are trying to do.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: sockets

Hi Kapil:

I'd add to Rick's comments. In order to compose messages (records) from the byte stream arriving on your socket, and achieve some fidelity to what you pass to your application, I generally prepend a header and append a trailer.

The header can include a character count (length inclusive string) which you then use to give a level of assurance that you received what you expected when you have found the trailer.

As framing characters we use 'stx' for the header; and the pair 'fs' & 'cr' for the trailer.

...JRF...
Kapil_2
Advisor

Re: sockets

Rick,
That piece of information was helpful.
Was there a reason for mentioning 1460 or was it just a random figure you mentioned?
Also what is the Nagle Algorithm and when does it come into picture?

Thanks to all of you for your replies.

Kapil
James R. Ferguson
Acclaimed Contributor

Re: sockets

Hi Kapel:

When short messages are transmitted with TCP/IP they are packed to avoid or reduce network congestion. The Nagle algorithm delays transmission of short messages to attempt to wait until more messages are available.

...JRF...
rick jones
Honored Contributor

Re: sockets

1460 is commonly the MSS (maximum segment size) for TCP connections over links with 1500 byte MTUs.

as for the rest - that is what Stevens' books are for !-) i suspect you could find boatloads of opinions on the nagle algorithm with a usenet search at groups.deja.com - some of them - the correct ones of course!-) would even be from me :)

there is no rest for the wicked yet the virtuous have no pillows