Jiaming Li
Jiaming Li

Reputation: 227

socket programming confusion of getaddrinfo function

In getaddrinfo("www.example.net","1234", &hints, &server_info)

What is the use of the hints parameter?

Upvotes: 0

Views: 303

Answers (1)

Malt
Malt

Reputation: 30285

getaddrinfo is documented in a man page, which has this to say regarding the hints parameter:

The hints argument points to an addrinfo structure that specifies criteria for selecting the socket address structures returned in the list pointed to by res. If hints is not NULL it points to an addrinfo structure whose ai_family, ai_socktype, and ai_protocol specify criteria that limit the set of socket addresses returned by getaddrinfo()

So it's a parameter that limits the results based on a given criteria. The criteria is given using a addrinfo struct:

struct addrinfo {
    int              ai_flags;
    int              ai_family;
    int              ai_socktype;
    int              ai_protocol;
    socklen_t        ai_addrlen;
    struct sockaddr *ai_addr;
    char            *ai_canonname;
    struct addrinfo *ai_next;
};

You can read about each of the parameters of the struct on the same man page.

Upvotes: 1

Related Questions