HPlogo HP-UX IPv6 Porting Guide: HP-UX 11i v2 September 2004 > Chapter 15 Sample Client/Server Programs

IPv6 TCP Client Using getaddrinfo() for Name/Service Lookup

» 

Technical documentation

Complete book in PDF

 » Table of Contents

This fragment of an IPv6 TCP Client is a port of the preceding IPv6 client, using getaddrinfo() rather than gethostbyname().

struct addrinfo *res, *ainfo;struct addrinfo hints;
/* clear out hints */memset ((char *)&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
error =  getaddrinfo(argv[1], "example", &hints, &res);
if (error != 0) {
fprintf(stderr, "%s: %s not found in name service database\n",
argv[0], argv[1]);
exit(1);
}
for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) {
/* Create the socket. */
s = socket (ainfo->ai_family,ainfo->ai_socktype,
ainfo->ai_protocol);
if (s == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
freeaddrinfo(res);
exit(1);
}
if (connect(s, ainfo->ai_addr, ainfo->ai_addrlen) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to connect to remote\n", argv[0]);
close(s);
continue;
}
else
break;
}
© 2004 Hewlett-Packard Development Company, L.P.