Everything2
Near Matches
Ignore Exact
Full Text
Everything2

struct

created by JeffMagnus

(thing) by JeffMagnus (4.9 y) (print)   ?   (I like it!) Sat Dec 25 1999 at 1:13:16

In C, the struct keyword defined a record similar to the record of Pascal and BASIC; however, in C++ a struct is much like a class only by default all members are public instead of private.

(thing) by iain (5.5 y) (print)   ?   (I like it!) Fri Jul 21 2000 at 11:58:24

Taken from the original at http://www.ecst.csuchico.edu/~beej/guide/net/ ... see end of writeup for Copyright statement.

structs

Well, we're finally here. It's time to talk about programming. In this section, I'll cover various data types used by the sockets interface, since some of them are a real bitch to figure out.

First the easy one: a socket descriptor. A socket descriptor is the following type:

int

Just a regular int.

Things get weird from here, so just read through and bear with me. Know this: there are two byte orderings: most significant byte (sometimes called an "octet") first, or least significant byte first. The former is called "Network Byte Order". Some machines store their numbers internally in Network Byte Order, some don't. When I say something has to be in NBO, you have to call a function (such as htons()) to change it from "Host Byte Order". If I don't say "NBO", then you must leave the value in Host Byte Order.

My First Struct(TM) -- struct sockaddr. This structure holds socket address information for many types of sockets:

struct sockaddr {
  unsigned short    sa_family;    /* address family, AF_xxx       */
  char              sa_data[14];  /* 14 bytes of protocol address */
};

sa_family can be a variety of things, but it'll be "AF_INET" for everything we do in this document. sa_data contains a destination address and port number for the socket. This is rather unwieldy.

To deal with struct sockaddr, programmers created a parallel structure: struct sockaddr_in ("in" for "Internet".)

struct sockaddr_in {
  short int          sin_family;  /* Address family               */
  unsigned short int sin_port;    /* Port number                  */
  struct in_addr     sin_addr;    /* Internet address             */
  unsigned char      sin_zero[8]; /* Same size as struct sockaddr */
};

This structure makes it easy to reference elements of the socket address. Note that sin_zero (which is included to pad the structure to the length of a struct sockaddr) should be set to all zeros with the function bzero() or memset(). Also, and this is the important bit, a pointer to a struct sockaddr_in can be cast to a pointer to a struct sockaddr and vice-versa. So even though socket() wants a struct sockaddr *, you can still use a struct sockaddr_in and cast it at the last minute! Also, notice that sin_family corresponds to sa_family in a struct sockaddr and should be set to "AF_INET". Finally, the sin_port and sin_addr must be in Network Byte Order!

"But," you object, "how can the entire structure, struct in_addr sin_addr, be in Network Byte Order?" This question requires careful examination of the structure struct in_addr, one of the worst unions alive:

/* Internet address (a structure for historical reasons) */
struct in_addr {
  unsigned long s_addr;
};

Well, it used to be a union, but now those days seem to be gone. Good riddance. So if you have declared "ina" to be of type struct sockaddr_in, then "ina.sin_addr.s_addr" references the 4 byte IP address (in Network Byte Order). Note that even if your system still uses the God-awful union for struct in_addr, you can still reference the 4 byte IP address in exactly the same way as I did above (this due to #defines.)


Prev | Up | Next


Copyright © 1995, 1996 by Brian "Beej" Hall. This guide may be reprinted in any medium provided that its content is not altered, it is presented in its entirety, and this copyright notice remains intact. Contact beej@ecst.csuchico.edu for more information.


printable version
chaos

C Structures Funny C declarations Record Object Orientation in ANSI C
reading and writing data structures in C Projects that use C techniques and call it C++ make me ill class typedef
typename Union the use of = in online communication C++ typedef
-> Java is prepared for Y10K malloc abuse CString
Beej's Guide to Network Programming MyBase and other VB.NET concepts designed to make you feel like a five year old C++: inconsistent syntax offsetof
C++: how big is nothing? member function structure WindowMaker
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Things you could have written:
In the empty mountains
Peak oil
Eyesight resolution
Heroes of Destiny
My meeting with Laetitia Casta
Everything Quest
Pastor Aeternus
ooblick
Temporary Autonomous Zone
Vlad Tepes
Marquis de Sade
Why I write
Installing LinuxPPC on a Powermac G4's second hard drive
New Writeups
Ysardo
a brother to a sister(personal)
antigravpussy
your warm whispers(personal)
Clarke
Multiculturalism(idea)
aneurin
Earl of Landaff(person)
Heitah
Pseudocide(idea)
XWiz
Google Knol(lede)
Mythi
July 24, 2008(personal)
locke baron
The fall of Earth(fiction)
BookReader
Fear the Cold(dream)
Pavlovna
Kathleen MacInnes(person)
stainedglass
1(fiction)
kalen
Three "T"s(idea)
octillion369
Undead(idea)
archiewood
Ico(fiction)
Heisenberg
Why I love Everything2(log)
This page courtesy of The Everything Development Company