Windows API Reference

Comprehensive Documentation for Windows Developers

Address Family

The address family determines the protocol that the socket uses. For example, an address family of AF_INET specifies the Internet Protocol version 4 (IPv4) protocol. An address family of AF_INET6 specifies the Internet Protocol version 6 (IPv6) protocol.

Constants

The following address families are commonly used:

Constant Description
AF_UNSPEC An unspecified address family. This can be used to indicate that no specific address family is required.
AF_INET The Internet Protocol version 4 (IPv4) protocol.
AF_INET6 The Internet Protocol version 6 (IPv6) protocol.
AF_IRDA The Infrared Data Association (IrDA) protocol.
AF_CHAOS The CHAOS protocol.
AF_NETBIOS The NetBIOS protocol.
AF_BTH The Bluetooth protocol.
AF_LINK The Link-layer interface.
AF_HYPERV The Microsoft Hyper-V hypervisor protocol.

Usage in Socket Functions

The address family is a parameter in several socket functions, such as socket() and bind(). When creating a socket, you specify the address family to determine the underlying network protocol.

Example: Creating an IPv4 TCP Socket

The following C++ code snippet demonstrates how to create a TCP socket for IPv4 using the socket() function.


#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")

int main() {
    WSADATA wsaData;
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET) {
        printf("Error at socket(): %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    printf("IPv4 TCP socket created successfully.\n");

    // ... other socket operations ...

    closesocket(sock);
    WSACleanup();
    return 0;
}
            

Related Topics