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.

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.

Log in or register to write something here or to contact authors.