linux 网络通信地址查询api

#include 
struct hostent* gethostent(void);

作用:返回本机主机信息。该函数读取/etc/hosts,逐行返回此文件中内容,即多次调用每次返回不同值,不可重入,示例:

struct hostent* p;
p = gethostent();
while (p) {
    printf("%s\n", p->h_name);
    p = gethostent();
}
struct protoent* getprotoent(void)

作用:返回协议信息。该函数读取/etc/protocols,逐行返回此文件中内容,示例:

struct protoent* p;
p = getprotoent();
while (p) {
    printf("%s\n", p->p_name);
    p = getprotoent();
}
struct servent* getservent(void)

作用:返回服务名。该函数读取/etc/services,逐行返回文件内容,示例:

struct servent* p;
p = getservent();
while (p) {
    printf("%s\n", p->s_name);
    p = getservent();
}
#include 
#include 
#include 

int getaddrinfo(
    const char* node,
    const char* service,
    const struct adddrinfo* hints,
    struct adddrinfo** res);

作用:整合上述单个函数的功能,提供一个一次获取所有信息的接口,同时此函数是线程安全的,示例:

struct addrinfo* p;
struct addrinfo hint;
hint.ai_flags = AI_CANONNAME;
getaddrinfo("localhost", "telnet", &hint, &p);
for (struct addrinfo* ploop = p; ploop; ploop = ploop->ai_next) {
    printf("%s\n", ploop->ai_canonname);
}

 

你可能感兴趣的:(linux)