Various structures are used in Unix Socket Programming to hold information about the address and port, and other information. Most socket functions require a pointer to a socket address structure as an argument. Structures defined in this chapter are related to Internet Protocol Family.
The first structure is sockaddr that holds the socket information −
struct sockaddr { unsigned short sa_family; char sa_data[14]; };
This is a generic socket address structure, which will be passed in most of the socket function calls. The following table provides a description of the member fields −
Attribute | Values | Description |
---|---|---|
sa_family |
AF_INET AF_UNIX AF_NS AF_IMPLINK |
It represents an address family. In most of the Internet-based applications, we use AF_INET. |
sa_data | Protocol-specific Address | The content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family, we will use port number IP address, which is represented by sockaddr_in structure defined below. |
The second structure that helps you to reference to the socket's elements is as follows −
struct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; };
Here is the description of the member fields −
Attribute | Values | Description |
---|---|---|
sa_family |
AF_INET AF_UNIX AF_NS AF_IMPLINK |
It represents an address family. In most of the Internet-based applications, we use AF_INET. |
sin_port | Service Port | A 16-bit port number in Network Byte Order. |
sin_addr | IP Address | A 32-bit IP address in Network Byte Order. |
sin_zero | Not Used | You just set this value to NULL as this is not being used. |
This structure is used only in the above structure as a structure field and holds 32 bit netid/hostid.
struct in_addr { unsigned long s_addr; };
Here is the description of the member fields −
Attribute | Values | Description |
---|---|---|
s_addr | service port | A 32-bit IP address in Network Byte Order. |
This structure is used to keep information related to host.
struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list #define h_addr h_addr_list[0] };
Here is the description of the member fields −
Attribute | Values | Description |
---|---|---|
h_name | ti.com etc. | It is the official name of the host. For example, howcodex.com, google.com, etc. |
h_aliases | TI | It holds a list of host name aliases. |
h_addrtype | AF_INET | It contains the address family and in case of Internet based application, it will always be AF_INET. |
h_length | 4 | It holds the length of the IP address, which is 4 for Internet Address. |
h_addr_list | in_addr | For Internet addresses, the array of pointers h_addr_list[0], h_addr_list[1], and so on, are points to structure in_addr. |
NOTE − h_addr is defined as h_addr_list[0] to keep backward compatibility.
This particular structure is used to keep information related to service and associated ports.
struct servent { char *s_name; char **s_aliases; int s_port; char *s_proto; };
Here is the description of the member fields −
Attribute | Values | Description |
---|---|---|
s_name | http | This is the official name of the service. For example, SMTP, FTP POP3, etc. |
s_aliases | ALIAS | It holds the list of service aliases. Most of the time this will be set to NULL. |
s_port | 80 | It will have associated port number. For example, for HTTP, this will be 80. |
s_proto |
TCP UDP |
It is set to the protocol used. Internet services are provided using either TCP or UDP. |
Socket address structures are an integral part of every network program. We allocate them, fill them in, and pass pointers to them to various socket functions. Sometimes we pass a pointer to one of these structures to a socket function and it fills in the contents.
We always pass these structures by reference (i.e., we pass a pointer to the structure, not the structure itself), and we always pass the size of the structure as another argument.
When a socket function fills in a structure, the length is also passed by reference, so that its value can be updated by the function. We call these value-result arguments.
Always, set the structure variables to NULL (i.e., '\0') by using memset() for bzero() functions, otherwise it may get unexpected junk values in your structure.