在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. |
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.