Graphics Programs Reference
In-Depth Information
From /usr/include/netinet/in.h
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */
/* Pad to size of 'struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};
The SOCKADDR_COMMON part at the top of the structure is simply the unsigned
short int mentioned above, which is used to define the address family. Since
a socket endpoint address consists of an Internet address and a port number,
these are the next two values in the structure. The port number is a 16-bit
short, while the in_addr structure used for the Internet address contains a
32-bit number. The rest of the structure is just 8 bytes of padding to fill out
the rest of the sockaddr structure. This space isn't used for anything, but must
be saved so the structures can be interchangeably typecast. In the end, the
socket address structures end up looking like this:
sockaddr structure (Generic structure)
Family
sa_data (14 bytes)
sockaddr_in structure (Used for IP version 4)
Family
Port #
IP address
Extra padding (8 bytes)
Both structures are the same size.
0x423
Network Byte Order
The port number and IP address used in the AF_INET socket address structure
are expected to follow the network byte ordering, which is big-endian. This is
the opposite of x 86's little-endian byte ordering, so these values must be con-
verted. There are several functions specifically for these conversions, whose
prototypes are defined in the netinet/in.h and arpa/inet.h include files. Here
is a summary of these common byte order conversion functions:
htonl( long value ) Host-to-Network Long
Converts a 32-bit integer from the host's byte order to network byte order
Search WWH ::




Custom Search