Search code examples
winsock2getaddrinfo

Conceptual query with Getaddrinfo method


When using the getaddrinfo method, I'm providing an IP address and the port number. I'm getting a linked list in the out variable. It works fine. But I have a conceptual doubt here. When i am giving both the IP address and port, how is it there are so many possible sockets returned? Shouldnt there be only one?


Solution

  • From the getaddrinfo man page:

    There are several reasons why the linked list may have more than one addrinfo structure, including: the network host is multihomed, accessible over multiple protocols (e.g. both AF_INET and AF_INET6); or the same service is available from multiple socket types (one SOCK_STREAM address and another SOCK_DGRAM address, for example). Normally, the application should try using the addresses in the order in which they are returned. The sorting function used within getaddrinfo() is defined in RFC 3484; the order can be tweaked for a particular system by editing /etc/gai.conf (available since glibc 2.5).