1、TCP/IP模型:
2、TCP/IP- 各层级网络协议(从下往上):
1)数据链路层:
ARP: 地址解析协议,用IP地址获取MAC地址的协议,通过ip的地址获取mac地
RARP: 逆地址解析协议, // zhangzx:反过来的,通过mac的地址获取ip地址
2)网络层:
IP:网际协议
ICMP:控制报文协议, ping
3)传输层:
TCP: 面向连接的可靠传输协议
UDP: 无连接的传输协议
4)应用层:
HTTP:
FTP:
TELNET:
自定义协议:
3、网络中传输的数据包:
1.ping //zhangzx:经常使用ping命令,就是icmp协议
数据包:mac层协议,ICMP协议
2.www.sohu.com
数据包:mac层协议,IP层协议,TCP层协议,HTTP协议
3.qt聊天室
数据包:mac层协议,IP层协议,TCP层协议,私聊消息结构体
4、以太网、ip、tcp、udp、icmp协议结构体定义:
#pragma once#pragma pack(1)typedef struct ether_header {unsigned char ether_dhost[6]; //目的MAC地址unsigned char ether_shost[6]; //源MAC地址unsigned short ether_type; //协议类型
}ETHHEADER, *PETHHEADER;typedef struct ipv4_header {unsigned char ver_ihl; //版本 (4 bits) + 首部长度 (4 bits)unsigned char tos; //服务类型unsigned short tlen; //数据报总长度unsigned short identification; //标识unsigned short flags_fo; //标志 (3 bits) + 片偏移 (13 bits)unsigned char ttl; //生存时间unsigned char proto; //协议unsigned short crc; //首部校验和unsigned int sourceIP; //源IP地址unsigned int destIP; //目的IP地址
}IPHEADER, *PIPHEADER;typedef struct tcp_header {unsigned short SourPort; //源端口号 unsigned short DestPort; //目的端口号unsigned int SeqNo; //序号unsigned int AckNo; //确认序号unsigned char HLen; //首部长度(保留位)unsigned char Flag; //标识(保留位)unsigned short Window; //窗口大小unsigned short ChkSum; //校验和unsigned short UrgPtr; //紧急指针
}TCPHEADER, *PTCPHEADER;typedef struct udp_header {unsigned short sport; //源端口号unsigned short dport; //目的端口号unsigned short len; //数据报长度unsigned short crc; //校验和
}UDPHEADER, *PUDPHEADER;typedef struct icmp_heard
{unsigned char icmp_type; //type of message(报文类型) unsigned char icmp_code; //type sub code(报文类型子码) unsigned short icmp_cksum; //校验和unsigned short icmp_id; //标识,发送和回收的得一致unsigned short icmp_seq;unsigned char icmp_data[1]; //数据, 1个字节其实是个占位符
}ICMPHEADER, *PICMPHEADER;#pragma pack()