请教socket编程中getnameinfo的用法问题
域名解析用的
/**
* getnameinfo()
* OS: 尘友含Ubuntu 11.04 Server
* for other examples, see man getnameinfo page
*/
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct sockaddr_in sa_in; 告衫
char host[NI_MAXHOST], service[NI_MAXSERV];
int flags;
int err;
sa_in.sin_family = AF_INET; // IPv4
sa_in.sin_port = htons(2049); // the port of NFS
inet_pton(AF_INET, "127.0.0.1", &sa_in.sin_addr.s_addr); // "220.181.111.86" is the address of baidu.com
flags = 0; // NI_NUMERICHOST | NI_NUMERICSERV
err = getnameinfo((struct sockaddr *)(&sa_in), sizeof(struct sockaddr),
host, sizeof(host), service, sizeof(service), flags);
if(err != 0)
{
gai_strerror(err);
exit(EXIT_FAILURE);
}
printf("host=%s, 派笑serv=%s\n", host, service);
return 0;
}
/*
$ ./a.out
host=localhost, serv=nfs
$ ./a.out
host=220.181.111.86, serv=nfs
*/