名字与地址转换相关函数

===============================================================
#include <netdb.h>

struct hostent * gethostbyname(const char * hostname);

--作用
查找主机名。当调用成功时,返回一个指向hostent结构
的指针,该结构中含有所查找主机的所有IPv4地址

--返回值
成功返回非空指针,出错返回NULL且设置h_errno

===============================================================
#include <netdb.h>

struct hostent * gethostbyaddr(const char *addr,socklen_t len, int family);

--作用
由一个二进制的IP地址找到相应的主机名

--返回值
成功返回非空指针,出错返回NULL且设置h_errno

===============================================================
struct servent {
char    *s_name;     // 服务名
char   **s_aliases;  // 别名列表 
int      s_port;     // 端口号,以网络字节序返回
char    *s_proto;    // 使用的协议
}

===============================================================
#include <netdb.h>

struct servent * getservbyname(const char * servname, 
const char * protoname);

--作用
根据给定名字查找相应服务

--返回值
成功返回非空指针,出错返回NULL

===============================================================
#include <netdb.h>

struct servent * getservbyport(int port, const char *protoname);

--作用
根据给定端口号和可选协议查找相应服务

--注意
port参数的值必须为网络字节序,函数的典型调用为:
struct servent * sptr = getservbyport(htons(53), "udp");

--返回值

成功返回非空指针,出错返回NULL

===============================================================
struct addrinfo {
int              ai_flags; 
int              ai_family;
int              ai_socktype;
int              ai_protocol;
socklen_t        ai_addrlen;
struct sockaddr *ai_addr;
char            *ai_canonname;
struct addrinfo *ai_next;
};

===============================================================
#include <netdb.h>

int getaddrinfo(const char *hostname, const char *service,
const struct addrinfo *hints, struct addrinfo **result);

--作用
处理名字到地址以及服务到端口这两种转换

--返回值
成功返回0,出错返回非0
===============================================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值