Unix Network Programming Manual
Prev Page Next Page

Addresses

In the example program and in the description of functions, you have seen references to a C strucure named sockaddr. This structure is defined in socket.h as: So this is a 16 byte structure (sa_family_t is an unsigned char) and it represents to the operating system what a host address looks like. It is general in the sense that the family designation allows for lots of different address types, as long they fit in the 14 byte sa_data field. After you choose a particular family, the sa_data field can be interpreted a number of ways. For example, Internet addresses are are 4 byte integer and the Xerox Network Software network uses a two-byte address.

To make things easier, the more common address types are given their own structure type. For example, the Internet address type is defined in /usr/include/netinet/in.h as: where in_port_t is an unsigned short (two-byte integer) and in_addr is an unsigned int (4 byte integer). Note that the last 8 bytes are set to zero and unused. When using this structure, the family should always be AF_INET. Since everything about the address is known from the family, it is not necessary to set the length.

There are two parts to this address, a port and a host address. The address part is defined by the Internet structure to be a 32-bit integer, which is normally displayed as four separate bytes. For example, 153.90.192.3 indicates that the address is 00110101010110101100000000000011. The most significant 8 bits are the integer 153, the second byte is 90, the third is 192 and the least significant byte is 3. The dotted-decimal notation is widely used and recognized for Internet addresses.

The Internet address represents the "name" of a host on the network. The port is also defined by the Internet to be the "name" of a process on a particular host. A host, such as 153.90.192.3, might have hundreds of processes running that want to communicate over the network. In order for the operating system to distinguish between them, each process has to specify a port that they want to use. When a process sends a message to a process on another machine, it sends its Internet host address and its port. When a response arrives, the operating system can tell by the port what process should receive it.

You might wonder how a process could ever send a message to another process without receiving a message telling it what port the process is using. For such things, well-known ports are used. These are port numbers that are assigned to specific types of servers. If you look in the file "/etc/services" on your machine, you will see records of the following type: These are assignments of specific processes, such as the ftp server (ftpd), to specific ports (21). If everyone agrees that such an assignment will be made on every host that runs ftp, a client will always be able to connect to it. There are other servers that are normally assigned ports that are not in the table, such as the web server that normally uses port 80. The first 1024 ports (0-1023) are reserved and can't be used by normal processes, but those above 1023 are available to any process.

The code in Example1 is going to connect to a server on the host "191.31.215.210" which is connected to port 32351. The calls to htons and inet_network will be discussed later, but the former guarantees that the port is in the correct internal format, and the latter converts dotted-decimal notation to an 4-byte integer.


Prev Page Next Page