Winsock Address Family
The address family defines the protocol family or set of protocols that a socket can use. Winsock supports multiple address families, allowing applications to be protocol-independent.
AF_INET
Represents the Internet Protocol version 4 (IPv4) address family. This is the most common address family used for TCP/IP networking.
Constants:
AF_INET
Example Usage:
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(80);
sa.sin_addr.s_addr = inet_addr("192.168.1.1");
AF_INET6
Represents the Internet Protocol version 6 (IPv6) address family. This is used for IPv6 addresses, offering a larger address space compared to IPv4.
Constants:
AF_INET6
Example Usage:
struct sockaddr_in6 sa6;
sa6.sin6_family = AF_INET6;
sa6.sin6_port = htons(8080);
// ... IPv6 address assignment ...
AF_UNSPEC
An unspecified address family. This allows the system to select an appropriate address family based on the protocol specified. It is often used when the protocol is unknown or when binding to any available address.
Constants:
AF_UNSPEC
Other Address Families
Winsock may also support other address families depending on the underlying network protocols installed on the system. These can include:
AF_IPX
(Novell NetWare IPX/SPX) - DeprecatedAF_NETBIOS
(NetBIOS) - DeprecatedAF_IRDA
(Infrared Data Association)AF_BTH
(Bluetooth)
It is recommended to use AF_INET
or AF_INET6
for modern network programming.
Selecting an Address Family
The choice of address family depends on the network protocol you intend to use. For standard internet communication, AF_INET
(for IPv4) or AF_INET6
(for IPv6) are the appropriate choices.
Related Functions
socket()
: Creates a socket and specifies the address family.bind()
: Associates a local address with a socket.connect()
: Establishes a connection to a remote host.