c_cpp getaddrinfo()程序示例。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp getaddrinfo()程序示例。相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int
lookup_host (const char *host)
{
struct addrinfo hints, *res;
int errcode;
char addrstr[100];
void *ptr;
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo (host, NULL, &hints, &res);
if (errcode != 0)
{
perror ("getaddrinfo");
return -1;
}
printf ("Host: %s\n", host);
while (res)
{
inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr, 100);
switch (res->ai_family)
{
case AF_INET:
ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
break;
case AF_INET6:
ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
break;
}
inet_ntop (res->ai_family, ptr, addrstr, 100);
printf ("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,
addrstr, res->ai_canonname);
res = res->ai_next;
}
return 0;
}
int main (void)
{
char inbuf[256];
int len;
do {
bzero(inbuf, 256);
printf("Type domain name:\n");
fgets(inbuf, 256, stdin);
len = strlen(inbuf);
inbuf[len-1] = '\0';
if(strlen(inbuf) > 0)
lookup_host (inbuf);
else
return EXIT_SUCCESS;
} while(1);
}
以上是关于c_cpp getaddrinfo()程序示例。的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp Midi程序变更示例
iPhone中的getaddrinfo
浅谈getaddrinfo函数的超时处理机制
C++ - getaddrinfo() - 使用动态 dns 获取自己的外部 IP 失败
Nodejs错误:getaddrinfo ENOENT [重复]
Rails 邮件发送:getaddrinfo:名称或服务未知