network data transfers scrambled

Mike Muuss mike at BRL.MIL
Thu Mar 28 11:32:50 AEST 1991


There is a slight chance that your problem may be due to not checking
the return code from your read() system call.  When sending data over
the network, it does not always arrive in the same size "chunks" as
you sent it in.  (The same thing can also happen on pipes, but is
seen less often, for a bunch of complicated reasons).

I attach a very handy subroutine written by Bob Miles which you can
use to aleviate this difficulty.  I hope this solves your problem.

	Best,
	 -Mike

------

/*
 *			M R E A D
 *
 * This function performs the function of a read(II) but will
 * call read(II) multiple times in order to get the requested
 * number of characters.  This can be necessary because pipes
 * and network connections don't deliver data with the same
 * grouping as it is written with.
 */
static int
mread(fd, bufp, n)
int fd;
register char	*bufp;
unsigned	n;
{
	register unsigned	count = 0;
	register int		nread;

	do {
		nread = read(fd, bufp, n-count);
		if(nread == -1)
			return(nread);
		if(nread == 0)
			return((int)count);
		count += (unsigned)nread;
		bufp += nread;
	 } while(count < n);

	return((int)count);
}



More information about the Comp.sys.sgi mailing list