preserving message boundaries
Don Libes
libes at cme.nbs.gov
Thu Jun 1 03:41:45 AEST 1989
There was some discussion earlier about preserving message boundaries,
and I posted two functions that would do this (by encoding the length
at the beginning). Namely:
> sized_write(fd,buffer,nbytes) - just like write()
> sized_read(fd,buffer,nbytes) - just like read() except that
> it returns what was written
> by one call to sized_write()
I pointed out that I used them on top of TCP (although they work over
anything). Someone wrote back that I should just be using send() and
recv() as this is what they were intended to do.
So I wrote a test program that loops, writing 2**n bytes, with n=n+1
each time through the loop. Another program reports what is being
atomically read.
Using sized_read(), it says:
reading 1 chars
reading 2 chars
reading 4 chars
reading 8 chars [everything looks hunky-dory here]
reading 16 chars
.
. [obvious lines omitted]
.
reading 65536 chars
reading 131072 chars
reading 262144 chars
-----------------------------------------------------------------------
Using recv() it says:
reading 1 chars
reading 2 chars
reading 3072 chars <--- ????!?!
reading 1020 chars <--- ????!?!
reading 4096 chars <--- ????!?!
reading 4096 chars
reading 4096 chars
reading 4096 chars
.
. [and so on]
.
---------------------------
recv() does not do atomic reads! Indeed, the man page says nothing
about it. The man page for send() says something very cryptic about
atomic writes - but even if it is atomic, it is worthless if recv()
isn't.
By the way, I am using SunOS 4.0.1. Should I report this to Sun? (It
is not clear from the man pages that this is incorrect behavior.)
Don Libes libes at cme.nbs.gov ...!uunet!cme-durer!libes
More information about the Comp.unix.wizards
mailing list