标签:
一个IP地址是有小数点分开的十进制数表示的,我们称为点分十进制表示法。其中每一个十进制数代表一个字节的无符号数值(按照网络字节序)因为每个字节都是无符号的8位数值,这就限制了每一个字节所能表示的范围是0~255。
特殊的IP
#include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct in_addr addr; if(argc != 2) { fprintf(stderr,"%s <dotted-address>\n", argv[0]); exit(EXIT_FAILURE); } if( (addr.s_addr = inet_addr(argv[1])) == -1) { fprintf(stderr,"%s is Invalid address\n",argv[1]); } else { fprintf(stdout,"%u\n",addr.s_addr); } return 0; } 注意:如果IP地址为 255.255.255.255 。那么调用 inet_addr() 函数后返回 -1 (因为 -1 的补码形式是 0xFFFFFFFF)。所以不建议使用 inet_addr() 函数。而使用 inet_aton() 函数。
#include <stdio.h> #include <arpa/inet.h> #include <stdlib.h> int main(int argc, char *argv[]) { if( argc != 2) { fprintf(stderr,"%s <dotted-address>\n",argv[0]); exit(EXIT_FAILURE); } struct in_addr addr; if(inet_aton(argv[1],&addr) != 0) fprintf(stdout,"%u\n",addr.s_addr); else fprintf(stdout,"%s Invalid address\n",argv[1]); return 0; }
#include <stdio.h> #include <arpa/inet.h> #include <stdlib.h> int main(int argc, char *argv[]) { if( argc != 2) { fprintf(stderr,"%s <dotted-address>\n",argv[0]); exit(EXIT_FAILURE); } struct in_addr addr; if(inet_aton(argv[1],&addr) == 0) { fprintf(stderr,"%s Invalid address\n",argv[1]); exit(EXIT_FAILURE); } printf("%s\n",inet_ntoa(addr)); return 0; }
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> int main(int argc, char *argv[]) { if( argc != 2 ) { fprintf(stderr,"%s <dotted-address>\n",argv[0]); exit(EXIT_FAILURE); } struct in_addr addr; if ((addr.s_addr = inet_network(argv[1])) == -1) { fprintf(stderr,"%s Invalid address\n",argv[1]); exit(EXIT_FAILURE); } else { fprintf(stdout,"%u \n",addr.s_addr); } return 0; }
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> int main(int argc, char *argv[]) { if( argc != 2 ) { fprintf(stderr,"%s <dotted-address>\n",argv[0]); exit(EXIT_FAILURE); } struct in_addr addr; if ((addr.s_addr = inet_network(argv[1])) == -1) { fprintf(stderr,"%s Invalid address\n",argv[1]); exit(EXIT_FAILURE); } addr.s_addr = htonl(addr.s_addr); fprintf(stdout,"0x%x \n",inet_lnaof(addr)); return 0; }
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> int main(int argc, char *argv[]) { if( argc != 2 ) { fprintf(stderr,"%s <dotted-address>\n",argv[0]); exit(EXIT_FAILURE); } struct in_addr addr; if ((addr.s_addr = inet_network(argv[1])) == -1) { fprintf(stderr,"%s Invalid address\n",argv[1]); exit(EXIT_FAILURE); } printf("0x%x\n",addr.s_addr); addr.s_addr = htonl(addr.s_addr); fprintf(stdout,"0x%x \n",inet_netof(addr)); return 0; }
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> int main(int argc, char *argv[]) { if( argc != 2 ) { fprintf(stderr,"%s <dotted-address>\n",argv[0]); exit(EXIT_FAILURE); } struct in_addr addr, addr_1; if ((addr.s_addr = inet_network(argv[1])) == -1) { fprintf(stderr,"%s Invalid address\n",argv[1]); exit(EXIT_FAILURE); } addr.s_addr = htonl(addr.s_addr); uint32_t net = inet_netof(addr); uint32_t host = inet_lnaof(addr); addr_1 = inet_makeaddr(net,host); printf("0x%x \n",htonl(addr_1.s_addr)); return 0; }
标签:
原文地址:http://blog.csdn.net/u011641885/article/details/46495089