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

IPv6 TCP Server using getaddrinfo() for Service Address Lookup

» 

Technical documentation

Complete book in PDF

 » Table of Contents

This code fragment is part of the example IPv6 server program that ships in the HP-UX 11i v2 /usr/lib/demos/networking/socket/af_inet6 directory, rewritten using the getaddrinfo() function call.

struct addrinfo *ainfo, *res;struct addrinfo hints;/* zero-out the hints before assignment */memset (&hints, 0, sizeof(hints));
. hints.ai_family = AF_INET6;
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(NULL, "example", &hints, &res); if (error != 0) {
fprintf(stderr, "%s: %s for service 'example'\n",
argv[0], gai_strerror(error));
exit(1);
}
/* Create the listen socket. */
ls = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
if (ls == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
exit(1);
}
/* Bind the listen address to the socket. */
if (bind(ls, res->ai_addr, res->ai_addrlen) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to bind address\n", argv[0]);
close(ls);
exit(1);
}
/* Initiate the listen on the socket so remote users
* can connect. The listen backlog is set to 5, which
* is within the supported range of 1 to 20.
*/
if (listen(ls, 5) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to listen on socket\n", argv[0]);
close(ls);
exit(1);
}
© 2004 Hewlett-Packard Development Company, L.P.