linux 网络通信地址查询api

#include <netdb.h>
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 <sys/typs.h>
#include <sys/socket.h>
#include <netdb.h>

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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值