linux网络地址转换函数
2011-06-25 01:37
381 查看
在socket编程时,会出现指定了ip,端口号,却连不上,这是因为网络地址跟本地主机地址的字符顺序不一样导致的。
在linux下提供了一些操作函数,如下:
unsigned long int htonl(unsigned long int hostlong);
用来将参数指定的32位hostlong 转换成网络字符顺序。
unsigned short int htons(unsigned short int hostshort);
用来将参数指定的16位hostshort转换成网络字符顺序。
unsigned long int inet_addr(const char *cp);
用来将参数cp所指的网络地址字符串转换成网络所使用的二进制数字。
网络地址字符串是以数字和点组成的字符串,例如:“163.13.132.68”。
int inet_aton(const char * cp,struct in_addr *inp);
用来将参数cp所指的网络地址字符串转换成网络使用的二进制的数字,然后存于参数inp所指的in_addr结构中。
char * inet_ntoa(struct in_addr in);
用来将参数in所指的网络二进制的数字转换成网络地址,然后将指向此网络地址字符串的指针返回
unsigned long inet_network(const char *addr);
这个函数需要一个在参数addr中包含一个点分隔的地址输入字符串。
返回值是主机顺序的IP地址的32位值
The inet_lnaof() function returns the local host address part of
the Internet address in. The local host address is returned in local host byte order.
The inet_netof() function returns the network number part of the Internet Address in.
The network number is returned in local host byte order.
在linux下提供了一些操作函数,如下:
unsigned long int htonl(unsigned long int hostlong);
用来将参数指定的32位hostlong 转换成网络字符顺序。
unsigned short int htons(unsigned short int hostshort);
用来将参数指定的16位hostshort转换成网络字符顺序。
unsigned long int inet_addr(const char *cp);
用来将参数cp所指的网络地址字符串转换成网络所使用的二进制数字。
网络地址字符串是以数字和点组成的字符串,例如:“163.13.132.68”。
int inet_aton(const char * cp,struct in_addr *inp);
用来将参数cp所指的网络地址字符串转换成网络使用的二进制的数字,然后存于参数inp所指的in_addr结构中。
char * inet_ntoa(struct in_addr in);
用来将参数in所指的网络二进制的数字转换成网络地址,然后将指向此网络地址字符串的指针返回
unsigned long inet_network(const char *addr);
这个函数需要一个在参数addr中包含一个点分隔的地址输入字符串。
返回值是主机顺序的IP地址的32位值
The inet_lnaof() function returns the local host address part of
the Internet address in. The local host address is returned in local host byte order.
The inet_netof() function returns the network number part of the Internet Address in.
The network number is returned in local host byte order.
相关文章推荐
- 网络地址转换相关函数使用(inet_addr,inet_ntoa,inet_addr)
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
- Linux网络地址转换源码分析
- Linux集群服务--网络地址转换
- 洞悉linux下的Netfilter&iptables:网络地址转换原理之SNAT
- (九)洞悉linux下的Netfilter&iptables:网络地址转换原理之DNAT
- Linux 2.4 NAT(网络地址转换) HOWTO(一)
- 洞悉linux下的Netfilter&iptables:网络地址转换原理之DNAT
- (十)洞悉linux下的Netfilter&iptables:网络地址转换原理之SNAT
- Linux 2.4 NAT (网络地址转换) HOWTO(二)
- 洞悉linux下的Netfilter&iptables:网络地址转换原理之SNAT
- 【linux网络编程】网络字节序、地址转换
- linux网络基础之三层交换机与NAT地址转换笔记
- Linux 2.4 NAT (网络地址转换) HOWTO(三)
- (九)洞悉linux下的Netfilter&iptables:网络地址转换原理之DNAT
- 网络地址转换函数总结
- Linux 网络编程——网络字节序、地址转换
- linux网络地址处理函数
- Linux Socket学习--地址转换函数
- Linux 2.4 NAT (网络地址转换) HOWTO(四)