Address Families

An address family is a group of protocols that support a common address structure. Winsock supports several address families, allowing applications to use different networking protocols.

Common Address Families

The most common address families supported by Winsock include:

  • AF_INET: Address family for the IPv4 protocol. This is the most widely used address family.
  • AF_INET6: Address family for the IPv6 protocol. This is the newer standard for internet addressing.
  • AF_UNSPEC: An unspecified address family. This can be used to allow the system to choose the most appropriate address family.
  • AF_IRDA: Address family for IrDA (Infrared Data Association) protocols.
  • AF_NETBIOS: Address family for NetBIOS protocols.

The ADDRESSFAMILY Structure

The address family is typically specified using the ADDRESSFAMILY data type, which is an integer. When defining network addresses, you will often use structures like sockaddr_in for IPv4 or sockaddr_in6 for IPv6, which implicitly or explicitly associate with their respective address families.

Example: Using AF_INET

When creating a socket, you specify the address family. Here's a C++ example of creating an IPv4 socket:


#include <winsock2.h>

// ...

SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
    // Handle error
}
                

Example: Using AF_INET6

And here's an example for creating an IPv6 socket:


#include <winsock2.h>

// ...

SOCKET sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
    // Handle error
}
                

getaddrinfo and Address Families

The getaddrinfo function is a powerful tool for resolving hostnames and service names into socket address structures. It allows you to specify desired address families and socket types to retrieve a list of suitable addresses.

When calling getaddrinfo, you can set the ai_family member of the addrinfo structure to:

  • AF_INET: To request IPv4 addresses.
  • AF_INET6: To request IPv6 addresses.
  • AF_UNSPEC: To request addresses for any available family (usually preferring IPv6 if available).

Related Concepts