Understanding the relationship between addrinfo object and sockaddr structure in Winsock

Gflockay-9238 0 Reputation points
2023-07-25T20:39:56.23+00:00

I came across an article on Winsock programming (https://learn.microsoft.com/en-us/windows/win32/winsock/creating-a-socket-for-the-client) and I am having trouble understanding the relationship between the addrinfo object and the sockaddr structure. Specifically, the article mentions that the addrinfo object contains a sockaddr structure, but I'm not sure what that means. Can someone help clarify this relationship and how it works in Winsock programming?

Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2023-07-26T03:22:33.17+00:00
    //
    //  Structure used in getaddrinfo() call
    //
    
    typedef struct addrinfo
    {
        int                 ai_flags;       // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
        int                 ai_family;      // PF_xxx
        int                 ai_socktype;    // SOCK_xxx
        int                 ai_protocol;    // 0 or IPPROTO_xxx for IPv4 and IPv6
        size_t              ai_addrlen;     // Length of ai_addr
        char *              ai_canonname;   // Canonical name for nodename
        _Field_size_bytes_(ai_addrlen) struct sockaddr *   ai_addr;        // Binary address
        struct addrinfo *   ai_next;        // Next structure in linked list
    }
    ADDRINFOA, *PADDRINFOA;
    
    
    INT WSAAPI getaddrinfo(
      [in, optional] PCSTR           pNodeName,
      [in, optional] PCSTR           pServiceName,
      [in, optional] const ADDRINFOA *pHints,
      [out]          PADDRINFOA      *ppResult
    );
    

    addrinfo structure is used in getaddrinfo() call as input and output which contains response information about the host.

    While The sockaddr structure contains information about the host and varies depending on the protocol selected. It then is used in the bind function , the connect function, etc.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.