TCP sockets, sending structures, and buffering: HELP!

jay dubb jdubb at bucsf.bu.edu
Sun Mar 10 06:50:26 AEST 1991


   I am posting this for a friend of mine who doesn't have access
to USENET, so please respond directly to mlevin at jade.tufts.edu.

   Hi -
I have the following problem. I have two short C programs using BSD4.3
sockets. They connect via a TCP connection, and then one of them sends
a bunch of structs to another, using write(socket,&struct,sizeof(struct))
calls. The other one does read(socket,&struct,sizeof(struct)) calls to
read the structures (defined in a common header file). There is no
problem as long as the structures are fairly small, but when they get
bigger, things get messed up - data is lost, what is read is not the
same size as what is written, data arrives corrupted, etc. Can anyone
enlighten me about how buffering in connected sockets should work? In
other words, if I want to exchange a bunch of structures of arbitrary
size, what should I do? Should I mess around with the buffer of the
socket using the setsockopt call?  I tried using a read routine found
in a book on Unix Networking, which basically reads in a loop until
everything is read. But, I don't understand how the reads behave when
multiple structures have been written. Does a read() return 0 between
structures? or only on socket drop (EOF)? Does anyone have a code
sample that will let you exchange multiple structures of arbitrary
size (less than 2K, by the way)? Thanks in advance for any advice.

			Mike Levin (mlevin at jade.tufts.edu)



More information about the Comp.unix.programmer mailing list