TCP/IP Programming Manual

the addrinfo structure. */
while (cur_info != NULL) {
if ((s = socket(cur_info->ai_family,cur_info->ai_socktype,0))<0){
perror("socket");
freeaddrinfo(server_info);
exit(3);
}
/* Connect to the server using the address in the addrinfo
structure named cur_info. */
if ((err = connect(s,cur_info->ai_addr,(int)cur_info->ai_addrlen))<0){
perror("connect");
cur_info = cur_info->ai_next;
continue;
}
break;
}
/* Free all addrinfo structures. */
freeaddrinfo(server_info);
if (err < 0)
exit(4);
/* Send a request to the server. */
if (send(s, request, (int)strlen(request), 0) < 0) {
perror("send");
exit(5);
}
/* Receive a response from the server. */
dcount = recv(s, databuf, sizeof(databuf), 0);
if (dcount < 0) {
perror("recv");
exit(6);
}
databuf[dcount] = '\0';
serveraddrlen = sizeof(serveraddr);
/* Obtain the address of the peer socket at the other end of the
connection and store the address in a sockaddr_in6 structure named
serveraddr. */
if (getpeername(s, (struct sockaddr*) &serveraddr, &serveraddrlen) < 0){
perror("getpeername");
exit(7);
}
printf("Response received from");
/* Obtain the server's name with a call to getnameinfo using the
address in the sockaddr_in6 structure named serveraddr. The
NI_NAMEREQD flag directs the routine to return a hostname for the
given address. */
ni = getnameinfo((struct sockaddr*)&serveraddr, serveraddrlen,
node, sizeof(node), NULL, 0, NI_NAMEREQD);
if (ni == 0)
printf(" %s", node);
ni = getnameinfo((struct sockaddr*)&serveraddr, serveraddrlen,
addrbuf, sizeof(addrbuf), NULL, 0, NI_NUMERICHOST);
if (ni == 0)
printf(" (%s)", addrbuf);
printf(":\n%s\n", databuf);
FILE_CLOSE_((short)s);
}
Using AF_INET6 Sockets 237