c

Definition

getaddrinfo (C)

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
 
int getaddrinfo(
	const char *restrict node,
	const char *restrict service, 
	const struct addrinfo *restrict hints,
	struct addrinfo **restrict res
);

Given node and service, which identify an Internet host and a service, getaddrinfo() returns one or more addrinfo structures, each of which contains an Internet address that can be specified in a call to bind or connect. The getaddrinfo() function combines the functionality provided by the gethostbyname and getservbyname functions into a single interface, but unlike the latter functions, getaddrinfo() is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.

Includes: types.h, socket.h, netdb.h

Examples

Example: HTTP Client

See: freeaddrinfo

int main(void) {
    struct addrinfo hints = {0};
    hints.ai_family = AF_UNSPEC;     // Allow IPv4 or IPv6
    hints.ai_socktype = SOCK_STREAM; // TCP
 
    struct addrinfo *res;
    if (getaddrinfo(argv[1], "80", &hints, &res) != 0) return 2;
	
	// ...
}