[comp.unix.wizards] TCP socket using BSD recv

TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu (03/19/90)

        I faced the similar problem as King when I used BSD recv() provided by
WIN/TCP for VMS Release 3.2. The maximum size received is only 4K for TCP.
But one thing puzzled me is I always get 1K in the very first recv() call,
only then I receive the remaining data, if the buffer size sent > 1K. For
example, if the client sends 5K of data, then the server's first recv() call
get 1K, and the second recv() will get the remaining 4K.
        Anybody has encountered the same problem before? Any helps is very
much appreciated. Thanks!

-Beng Hang Tay (email address: taybengh@nusdiscs.bitnet)
 Department of Information Systems and Computer Science
 National University of Singapore

jnixon@andrew.ATL.GE.COM (John F Nixon) (03/20/90)

TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
>        I faced the similar problem as King when I used BSD recv() provided by
>WIN/TCP for VMS Release 3.2. The maximum size received is only 4K for TCP.
>But one thing puzzled me is I always get 1K in the very first recv() call,
>only then I receive the remaining data, if the buffer size sent > 1K. For
>example, if the client sends 5K of data, then the server's first recv() call
>get 1K, and the second recv() will get the remaining 4K.


It sounds like you are using SOCK_STREAM sockets.  The semantics of
SOCK_STREAM are such that you are *not* guaranteed to receive all the
bytes of a write with a single read (especially with "large" writes).
You are responsible for maintaining record boundaries with SOCK_STREAM
sockets.  Either write fixed size records (usually not advisable), or
prepend a size field to your records.  Then read in a loop till all
of the data arrives.  Pseudo-code is:

	/* record size in recsize */
	byteshere = 0;
	while ( byteshere < recsize ) {
		bytes = read (soc, buf + byteshere, recsize - byteshere);
		if ( bytes < 0 ) {
			error stuff...
		}

		byteshere += bytes;
	}

You get the idea...

--
		
----
jnixon@atl.ge.com                    ...steinmetz!atl.decnet!jnxion