您的位置:首页 > 理论基础 > 计算机网络

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.

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: