Linux socket(第三章)

来源:互联网 发布:开个淘宝店要投资多少 编辑:程序博客网 时间:2024/05/21 17:58
  1.   IP地址是一个32位的二进制数,它由点分十进制记法表示,每一个十进制数代表一个8位无符号整型数,所以范围在0-255之间。
  2. 一个IP地址分为网络地址和主机地址,网络地址区分不同网络,主机地址区分相同网络中不同主机。不同的IP地址类他们的主机网络地址也是不固定的,A,B,C类地址定义了某一主机特定的IP地址,D类为组播地址,E类为保留今后所用。具体的划分是:           
    view plaincopy to clipboardprint?
    1. 分类  最低             最高                网络位        主机位   
    2. A    0.0.0.0      127.255.255.255    7           24   
    3. B    128.0.0.0    191.255.255.255    14          16   
    4. C    192.0.0.0    223.255.255.255    21          8   
    5. D    224.0.0.0    239.255.255.255    28          N/A   
    6. E    240.0.0.0    247.255.255.255    27          N/A  
           
  3. 网络掩码的作用在于把网络地址从IP地址中提取出来,实际上代表网络掩码的IP号与某一特定的IP地址进行“按位与”。           
    view plaincopy to clipboardprint?
    1. 分类            最低              最高               网络掩码   
    2. A            0.0.0.0         127.255.255.255  24   
    3. B            128.0.0.0       191.255.255.255  16   
    4. C            192.0.0.0       223.255.255.255  8  
  4. 以下是对IP地址进行检查和分类的源代码
  5. view plaincopy to clipboardprint?
    1. #include <stdio.h>   
    2. #include <unistd.h>   
    3. #include <stdlib.h>   
    4. #include <sys/types.h>   
    5. #include <sys/socket.h>   
    6. #include <netinet/in.h>   
    7.   
    8. int main(int argc, char **argv)   
    9. {   
    10.     int x;   
    11.     struct sockaddr_in adr_inet;   
    12.     int len_inet;   
    13.     unsigned msb;   
    14.     char class;   
    15.     char *netmask;   
    16.     static struct {   
    17.     unsigned char ip[4];   
    18.     } addresses[] = {   
    19.     { {   
    20.     44, 135, 86, 12}}, { {   
    21.     127, 0, 0, 1}}, { {   
    22.     172, 16, 23, 95}}, { {   
    23.     192, 168, 9, 1}},};   
    24.   
    25.     for (x = 0; x < 4; ++x) {   
    26.     memset(&adr_inet, 0, sizeof(adr_inet));   
    27.     adr_inet.sin_family = AF_INET;   
    28.     adr_inet.sin_port = htons(9000);   
    29.     memcpy(&adr_inet.sin_addr.s_addr, addresses[x].ip, 4);   
    30.     len_inet = sizeof(adr_inet);   
    31.   
    32.     msb = *(unsigned char *) &adr_inet.sin_addr.s_addr;   
    33.   
    34.     if ((msb & 0x80) == 0x00) {   
    35.         class = 'A';   
    36.         netmask = "255.255.255.0";   
    37.     } else if ((msb & 0xC0) == 0x80) {   
    38.         class = 'B';   
    39.         netmask = "255.255.255.0";   
    40.     } else if ((msb & 0xE0) == 0xC0) {   
    41.         class = 'C';   
    42.         netmask = "255.255.255.0";   
    43.     } else if ((msb & 0xF0) == 'D') {   
    44.         class = 'D';   
    45.         netmask = "255.255.255.255";   
    46.     } else {   
    47.         class = 'E';   
    48.         netmask = "255.255.255.255";   
    49.     }   
    50.     printf("Address %u.%u.%u.%u is class %c "  
    51.            "netmask %s/n",   
    52.            addresses[x].ip[0],   
    53.            addresses[x].ip[1],   
    54.            addresses[x].ip[2], addresses[x].ip[3], class, netmask);   
    55.     }   
    56.     return 0;   
    57. }  
  6. 为了减少字符串形式的IP地址转换成可用的套接口地址这样的编程负担,系统提供了一些转换函数。

          inet_aton(3)函数

         

view plaincopy to clipboardprint?
  1. #include <sys/socket.h>   
  2. #include <netinet/in.h>   
  3. #include <arpa/inet.h>   
  4.   
  5. int inet_aton(const char* string , struct in_addr *addr );  

 inet_ntoa(3)函数  

view plaincopy to clipboardprint?
  1. #include <sys/socket.h>   
  2. #include <netinet/in.h>   
  3. #include <arpa/inet.h>   
  4.   
  5. char *inet_ntoa(struct in_addr addr);  

    inet_network(3)函数:该函数的作用是将点分十进制记法的IP转换成主机字节序的32位二进制地址

view plaincopy to clipboardprint?
  1. #include <sys/socket.h>   
  2. #include <netinet.h>   
  3. #include <arpa/inet.h>   
  4.   
  5. unsigned long inet_network(const char *addr);  

view plaincopy to clipboardprint?
  1. #include <stdio.h>   
  2. #include <unistd.h>   
  3. #include <stdlib.h>   
  4. #include <sys/types.h>   
  5. #include <sys/socket.h>   
  6. #include <netinet/in.h>   
  7. #include <arpa/inet.h>   
  8.   
  9. int main (int argc, char **argv)   
  10. {   
  11.     int x;   
  12.     const char *addr[]={   
  13.         "44.135.86.12",   
  14.         "127.0.0.1",   
  15.         "172.16.23.95",   
  16.         "192.168.9.1"  
  17.     };   
  18.     unsigned long net_addr;   
  19.        
  20.     for(x=0;x<4;x++){   
  21.         net_addr = inet_network(addr[x]);   
  22.         printf("%s = 0x%08lX net 0x%08lX/n",   
  23.             addr[x],net_addr,(unsigned long)htonl(net_addr)   
  24.         );   
  25.     }   
  26.     return 0;   
  27. }  

 

          

原创粉丝点击