Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Nov 1996 14:37:11 -0600 (CST)
From:      Joe Greco <jgreco@brasil.moneng.mei.com>
To:        fenner@parc.xerox.com (Bill Fenner)
Cc:        terry@lambert.org, jdp@polstra.com, scrappy@ki.net, jgreco@brasil.moneng.mei.com, hackers@freebsd.org
Subject:   Re: Sockets question...
Message-ID:  <199611152037.OAA28878@brasil.moneng.mei.com>
In-Reply-To: <96Nov15.112419pst.177557@crevenia.parc.xerox.com> from "Bill Fenner" at Nov 15, 96 11:24:14 am

next in thread | previous in thread | raw e-mail | index | archive | help
> In message <199611151706.KAA26239@phaeton.artisoft.com> Terry wrote:
> >How do I read into a structure on a machine that demands aligned
> >data access?
> 
> You read into an intermediate buffer and copy it.  You have to convert from 
> network to machine representation anyway, so this isn't (much) more overhead.
> Or you use UDP if you want a record-oriented protocol.

Actually...  I usually found it easier to do the following.


#include	<stdio.h>

/*
 * One dull variant of Joe's "xread" function.
 */

int xread(fd, buf, siz)
int fd;
char *buf;
int siz;
{
        int rval;
        int chrs = 0;

        while (siz) {
                if ((rval = read(fd, buf, siz)) < 0) {
                        return(rval);
                }
                chrs += rval;
                siz -= rval;
                buf += rval;
        }
}

int main()
{
	int rval;
	struct big_ugly {
		yadda;
		yadda;
		yadda;
	} data;
	
	if ((rval = xread(fd, (char *)&data, sizeof(data))) != sizeof(data)) {
		fprintf(stderr, "Help me, I am on fire, xread returned %d\n",
						rval);
	}
}

This of course assumes you either do not need to do byte reordering, or do
it elsewhere.

... JG



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199611152037.OAA28878>