深入浅出ip地址!( 二 )


这里有一个小技巧来区分后俩个函数,a表示地址,n表示网络,所以就很好区分了 。
下面举例一个实际代码案例:
#include #include #include #include #include #include #include #include int main(){unsigned int addr = inet_addr("1.2.3.4");struct in_addr addr1 = {0x09080706};struct in_addr addr2 = {0x05040302};char *s1 = strcpy(malloc(32),inet_ntoa(addr1));char *s2 =strcpy(malloc(32),inet_ntoa(addr2));printf("addr = %x\n",addr);printf("addr1 = %s\n",s1);printf("addr2 = %s\n",s2);printf("s1 = %s\n",s1);printf("s2 = %s\n",s2);if(inet_aton("1.2.3.4"),&addr1)){printf("addr1 = %x\n",addr1.s_addr);}free(s1);free(s2);return 0;}
运行结果如下:
【深入浅出ip地址!】root@ubuntu:/home/hao/socket# ./a.out addr = 4030201addr1 = 6.7.8.9addr2 = 2.3.4.5s1=6.7.8.9s2=2.3.4.5addr1 = 4030201