Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uwm.edu!bionet!agate!ucbvax!MCIMAIL.COM!0004219666 From: 0004219666@MCIMAIL.COM (Bob Stine) Newsgroups: comp.protocols.tcp-ip Subject: Re: Problems with TCP, read(), and write() Message-ID: <03910404143730.0004219666NB1EM@mcimail.com> Date: 4 Apr 91 14:37:00 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 42 >I am having problems with TCP, write(), and read(). I want to send and >receive 32 byte structures.... >My problem is that if I do no put a sizeable delay in the >send or receive loop, I end up receiving only part of a 32 byte chunk >after a number of chunks have been received. Shawn, As you have discovered, TCP does not preserve record boundaries. One work-around makes use the value returned by read(), which is the number of the bytes read. Keep reading until you get your entire 32 bytes. I.e., int bf_ln, /* number of bytes requested */ bc, /* byte count returned by read() */ s; /* socket */ struct shawns_struct { /* whatever, 32 bytes worth */} in_rec; char *bptr; /* get the connection. Then, to load a structure... */ bf_ln = sizeof(struct shawns_struct); bptr = (char *) in_rec; while (bf_ln) { bc = read(s,bptr,bf_ln); if (bc < 1) exit(-1); bf_ln -= bc; bptr += bc; } Note that I exit if read() returns a value of zero. In BSD, select() will indicate that a closed socket is ready to read, but read() will return zero bytes. Hence, if the above loop didn't exit when read() returned a zero, then a closed socket would keep it busy for quite some time... :-) Regards, Bob Stine bstine@MCIMail.com