home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!ukma!darwin.sura.net!jvnc.net!princeton!tex.Princeton.EDU!subbarao
- From: subbarao@fc.hp.com (Kartik Subbarao)
- Subject: Re: Proper way to read/write a struct from/to a socket
- Message-ID: <1992Nov12.151114.3244@Princeton.EDU>
- Sender: news@Princeton.EDU (USENET News System)
- Nntp-Posting-Host: tex.princeton.edu
- Reply-To: subbarao@fc.hp.com
- Organization: putchar('I'); for (i = 0; i < 3; i++) putchar('E');
- References: <60p137_@rpi.edu>
- Date: Thu, 12 Nov 1992 15:11:14 GMT
- Lines: 18
-
- In article <60p137_@rpi.edu> cecchinr@gehrig.cs.rpi.edu (Ron Cecchini) writes:
- >Hi all,
- >
- >o read()/write() expect you to read/write a "char *buf"
- >o I have a variable "foo" which is of type "struct foo_type"
- >o I've been reading/writing "(char *)&foo"
- >
- >It seems to be working. Am I just lucky, or is this correct?
- >I've never tried reading into a structure like this before...
-
- Sure, it's fine. Think about it. The read() and write() calls transfer
- some bytes from where you tell it to start. C stores the elements
- in a struct contiguously in memory, so when you give the address of the struct
- as the starting address, then say sizeof struct for the number of bytes to
- be sent, and assuming the other end has the same byte-ordering scheme, then
- you can read it right into another struct.
-
- -Kartik
-