Linux socket(第三章)
来源:互联网 发布:开个淘宝店要投资多少 编辑:程序博客网 时间:2024/05/21 17:58
- 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;
- }
- Linux socket(第三章)
- 实战 Linux socket 学习笔记(第三章)
- 学习笔记《实战Linux Socket编程》第三章 地址转换函数
- Linux Socket Programming by Example-第三章 IP地址与子网划分
- 第三章 Linux 系统管理(中)
- 第三章 Linux 系统管理(上)
- Linux 第三章
- Linux第三章
- Linux程序设计第三章
- linux socket学习笔记(第二章)
- linux socket学习笔记(第二章)
- Socket 通信(Linux)
- UNIX Network阅读笔记 第三章socket介绍
- linux 设备驱动第三章
- linux系统编程 第三章
- 第三章 Linux文件管理
- Linux内核探讨-- 第三章
- Linux内核第三章作业
- 从现在起
- 能考察编程能力的C语言试题
- 嵌入式C语言笔试题
- 下一步该怎么走?
- Socket AM2、AM2+和AM3的一路进化
- Linux socket(第三章)
- /r 与 /n
- 有关数据冗余-sql语言艺术
- html的一些标记
- winform导入数据
- web网站中常用的javascript
- 英语文摘: 比尔盖茨的十大人生规则
- 哈佛图书馆墙上的话
- 系统程序员成长计划-工程管理(三)