198211401182 发表于 2015-11-24 01:01:06

inet_pton inet_ntop 注意点使用

#include <string.h>//bzero memset
#include <netinet/in.h>//struct in_addr
#include <sys/socket.h>//AF_INET
#include <arpa/inet.h>
int
main(int argc, char **argv)
{  char str[]=&quot;172.16.10.196&quot;;//
char str1;
struct in_addr myin;
int ret = 1;
  unsigned long lip = 0xac100ac4;


ret = inet_pton(AF_INET,str,&myin);                      //转换出来的myin是网络序的
printf(&quot;%2X/n&quot;,myin.s_addr);
myin.s_addr = ntohl(myin.s_addr);
printf(&quot;%2X/n&quot;,myin.s_addr);


printf(&quot;%2X/n&quot;,lip);
lip = htonl(lip);                                                         //要先将lip转换成网络序的
printf(&quot;%2X/n&quot;,lip);
ret = inet_ntop(AF_INET,&lip,str1,__SOCK_SIZE__);
printf(&quot;%s/n&quot;,str1);


exit(0);
}
  inet_pton的输出是网络序的
  inet_ntop的输入是网络序的
inet_ntop()是协议无关的地址转换函数,要考虑可重入性
页: [1]
查看完整版本: inet_pton inet_ntop 注意点使用