名字与地址转换相关函数

===============================================================
#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
===============================================================

猜你喜欢

转载自blog.csdn.net/liang_gu/article/details/80773709