From owner-freebsd-hackers Fri Nov 15 12:44:04 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id MAA14823 for hackers-outgoing; Fri, 15 Nov 1996 12:44:04 -0800 (PST) Received: from brasil.moneng.mei.com (brasil.moneng.mei.com [151.186.109.160]) by freefall.freebsd.org (8.7.5/8.7.3) with ESMTP id MAA14802 for ; Fri, 15 Nov 1996 12:43:56 -0800 (PST) Received: (from jgreco@localhost) by brasil.moneng.mei.com (8.7.Beta.1/8.7.Beta.1) id OAA28878; Fri, 15 Nov 1996 14:37:12 -0600 From: Joe Greco Message-Id: <199611152037.OAA28878@brasil.moneng.mei.com> Subject: Re: Sockets question... To: fenner@parc.xerox.com (Bill Fenner) Date: Fri, 15 Nov 1996 14:37:11 -0600 (CST) Cc: terry@lambert.org, jdp@polstra.com, scrappy@ki.net, jgreco@brasil.moneng.mei.com, hackers@freebsd.org In-Reply-To: <96Nov15.112419pst.177557@crevenia.parc.xerox.com> from "Bill Fenner" at Nov 15, 96 11:24:14 am X-Mailer: ELM [version 2.4 PL24] Content-Type: text Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk > 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 /* * 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