First page Back Continue Last page Overview Graphics
Example of getaddrinfo() for Clients
struct addrinfo hints, *res, *ai;
int s;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (gai = getaddrinfo(name, service, &hints, &res)) {
printf("getaddrinfo failed: %s\n", gai_strerror(gai)); return -1;
}
for (ai = res; ai != NULL; ai = ai->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) continue;
if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
close(s);
s = -1;
continue;
}
break;
}
freeaddrinfo(res);
return s;
Notes: