Saturday, October 1, 2011

struct sockaddr

Examining the Generic Socket Address

Because the BSD socket interface was developed before the ANSI C standard was adopted, there was no (void *) data pointer type to accept any structure address. Consequently, the BSD solution chosen was to define a generic address structure. The generic structure is defined by the C language statement

Example
Listing 2.1: The Generic Socket Address
#include <sys/socket.h>
struct sockaddr {
sa_family_t sa_family; /* Address Family */
char sa_data [14];     /* Address data. */
};

Presently the data type sa_family_t is an unsigned short integer, which is two bytes in length under Linux. The total structure size is 16 bytes. The structure element sa_data[14] represents 14 remaining bytes of address information.

Figure 2.1 provides a physical view of the generic socket address structure.
Figure 2.1:

Here is a representation of the generic socket address layout.
The generic socket address structure itself is not that useful to the programmer. It does, however, provide a reference model from which all other address structures must fit. For example, you will learn that all addresses must define the sa_family member in exactly the same location in the structure, because this element determines how the remaining bytes of the address are interpreted.

See Also:

No comments:

Post a Comment