函数说明参考网址
http://linux.die.net/man/3/<function_name> (library call)
#include <netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
作用:将主机域名转成IP地址。
参数:主机域名。
返回值:hostent结构体,如下:
NULL表示失败。
struct hostent {
char *h_name;
char **h_aliases;
int
h_addrtype;
int
h_length;
char **h_addr_list;
}
#define h_addr h_addr_list[0]
额外解释:在Linux中可以使用ping www.baidu.com返回该域名对应的IP地址。
#include <sys/socket.h>
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);
作用:将
IP地址
转成
主机域名
。
参数:IP地址
。
返回值:hostent结构体。
NULL表示失败。
额外解释:
The
gethostbyname*
() and
gethostbyaddr*
() functions are obsolete. Applications should use
getaddrinfo
(3) and
getnameinfo
(3) instead.
#include <netdb.h>
struct protoent *getprotoent(void);
作用:
得到系统所支持的所有协议。
返回值:protoent结构体,如下:NULL表示失败。
struct protoent {
char *p_name;
char **p_aliases;
int
p_proto;
}
#include <netdb.h>
struct protoent *getprotobyname(const char *name);
作用:
根据协议名字得到该协议的信息。
参数:协议名字
返回值:protoent结构体,
NULL表示失败。
#include <netdb.h>
struct protoent
*getprotobynumber(int proto);
作用:
根据协议号得到该协议的信息。
参数:协议号
返回值:protoent结构体,
NULL表示失败。
#include <netdb.h>
struct servent *getservent(void);
作用:
得到系统所支持的所有服务。
返回值:servent结构体,如下:NULL表示失败。
struct servent {
char *s_name;
char **s_aliases;
int
s_port;
char *s_proto;
}
#include <netdb.h>
struct servent *getservbyname(const char *name, const char *proto);
作用:
根据服务名和所使用的协议名得到该服务的信息。
参数:name表示服务名。proto表示协议名。
返回值:protoent结构体,
NULL表示失败。
#include <netdb.h>
struct servent *getservbyport(int port, const char *proto);
作用:
根据端口号和所使用的协议名得到该端口所对应的服务信息。
参数:port表示端口号,使用时需使用htons()进行转换。proto表示协议名。
返回值:protoent结构体,
NULL表示失败。
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef uint32_t in_addr_t;
in_addr_t inet_addr(const char *cp);
作用:将网络IP地址转换成无符号整型数。
参数:网络IP地址。(比如192.168.0.1)
返回值:
无符号整型数。
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
};
char *inet_ntoa(struct in_addr in);
作用:将
无符号整型数
转换成
网络IP地址
。
参数:in表示in_addr结构体对象。
返回值:网络IP地址
。(点分十进制格式)
#include <arpa/inet.h>或#include <netinet/in.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
作用:将计算机中的32位长整型数转换为网络中的32为长整型数。
参数:
计算机中的
32位长整型数
返回值:
网络中的32为长整型数。
额外解释:
计算机中的字符存储顺序与网络中的字符存储顺序有时是不同的。比如
On the i386 the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first.
#include <netdb.h>
extern int h_errno;
#include <sys/socket.h>
const char *hstrerror(int err);
作用:根据指定的错误号得到对应的错误信息。
参数:
指定的错误号。
返回值:错误字符串。