- IP地址是一个32位的二进制数,它由点分十进制记法表示,每一个十进制数代表一个8位无符号整型数,所以范围在0-255之间。
- 一个IP地址分为网络地址和主机地址,网络地址区分不同网络,主机地址区分相同网络中不同主机。不同的IP地址类他们的主机网络地址也是不固定的,A,B,C类地址定义了某一主机特定的IP地址,D类为组播地址,E类为保留今后所用。具体的划分是:
- 分类 最低 最高 网络位 主机位
- A 0.0.0.0 127.255.255.255 7 24
- B 128.0.0.0 191.255.255.255 14 16
- C 192.0.0.0 223.255.255.255 21 8
- D 224.0.0.0 239.255.255.255 28 N/A
- E 240.0.0.0 247.255.255.255 27 N/A
- 网络掩码的作用在于把网络地址从IP地址中提取出来,实际上代表网络掩码的IP号与某一特定的IP地址进行“按位与”。
- 分类 最低 最高 网络掩码
- A 0.0.0.0 127.255.255.255 24
- B 128.0.0.0 191.255.255.255 16
- C 192.0.0.0 223.255.255.255 8
- 以下是对IP地址进行检查和分类的源代码
-
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- int main(int argc, char **argv)
- {
- int x;
- struct sockaddr_in adr_inet;
- int len_inet;
- unsigned msb;
- char class;
- char *netmask;
- static struct {
- unsigned char ip[4];
- } addresses[] = {
- { {
- 44, 135, 86, 12}}, { {
- 127, 0, 0, 1}}, { {
- 172, 16, 23, 95}}, { {
- 192, 168, 9, 1}},};
- for (x = 0; x < 4; ++x) {
- memset(&adr_inet, 0, sizeof(adr_inet));
- adr_inet.sin_family = AF_INET;
- adr_inet.sin_port = htons(9000);
- memcpy(&adr_inet.sin_addr.s_addr, addresses[x].ip, 4);
- len_inet = sizeof(adr_inet);
- msb = *(unsigned char *) &adr_inet.sin_addr.s_addr;
- if ((msb & 0x80) == 0x00) {
- class = 'A';
- netmask = "255.255.255.0";
- } else if ((msb & 0xC0) == 0x80) {
- class = 'B';
- netmask = "255.255.255.0";
- } else if ((msb & 0xE0) == 0xC0) {
- class = 'C';
- netmask = "255.255.255.0";
- } else if ((msb & 0xF0) == 'D') {
- class = 'D';
- netmask = "255.255.255.255";
- } else {
- class = 'E';
- netmask = "255.255.255.255";
- }
- printf("Address %u.%u.%u.%u is class %c "
- "netmask %s/n",
- addresses[x].ip[0],
- addresses[x].ip[1],
- addresses[x].ip[2], addresses[x].ip[3], class, netmask);
- }
- return 0;
- }
- 为了减少字符串形式的IP地址转换成可用的套接口地址这样的编程负担,系统提供了一些转换函数。
inet_aton(3)函数
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- int inet_aton(const char* string , struct in_addr *addr );
inet_ntoa(3)函数
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- char *inet_ntoa(struct in_addr addr);
inet_network(3)函数:该函数的作用是将点分十进制记法的IP转换成主机字节序的32位二进制地址
- #include <sys/socket.h>
- #include <netinet.h>
- #include <arpa/inet.h>
- unsigned long inet_network(const char *addr);
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- int main (int argc, char **argv)
- {
- int x;
- const char *addr[]={
- "44.135.86.12",
- "127.0.0.1",
- "172.16.23.95",
- "192.168.9.1"
- };
- unsigned long net_addr;
- for(x=0;x<4;x++){
- net_addr = inet_network(addr[x]);
- printf("%s = 0x%08lX net 0x%08lX/n",
- addr[x],net_addr,(unsigned long)htonl(net_addr)
- );
- }
- return 0;
- }
本文介绍了IP地址的基本概念,包括其组成、分类及其对应的网络掩码,并提供了IP地址分类的示例代码。此外,还详细讲解了几种常用的IP地址转换函数,如inet_aton、inet_ntoa和inet_network。

被折叠的 条评论
为什么被折叠?



