Ethernet IP TCP UDP 协议头部格式

简介:

The Ethernet header structure is shown in the illustration below:

以太网头部14 bytes

Destination Source Len Data unit + pad FCS
(6 bytes) (6 bytes) (2) (46-1500 bytes) (4 bytes)
Ethernet header structure

The IP header structure is as follows:

IP头部20 bytes

4
8
16
32 bits
Ver. IHL Type of service Total length
Identification Flags Fragment offset
Time to live Protocol Header checksum
Source address
Destination address
Option + Padding
Data
IP header structure

The TCP header structure is as follows:

TCP头部20 bytes

16
32 bits
Source port Destination port
Sequence number
Acknowledgement number
Offset Resrvd U A P R S F Window
Checksum Urgent pointer
Option + Padding
Data
TCP header structure

The UDP header structure is shown as follows:

UDP头部8 bytes

16
32 bits
Source port Destination port
Length Checksum

Data
UDP header structure

一些协议头部的定义:

复制代码
struct ip 
{
#if BYTE_ORDER == LITTLE_ENDIAN
    u_char    ip_hl:4,        /* header length */
              ip_v:4;            /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
    u_char    ip_v:4,            /* version */
              ip_hl:4;        /* header length */
#endif
    u_char    ip_tos;            /* type of service */
    short    ip_len;            /* total length */
    u_short    ip_id;            /* identification */
    short    ip_off;            /* fragment offset field */
    u_char    ip_ttl;            /* time to live */
    u_char    ip_p;            /* protocol */
    u_short    ip_sum;            /* checksum */
    struct    in_addr ip_src,ip_dst;    /* source and dest address */
};

struct udphdr 
{
    u_short    uh_sport;        /* source port */
    u_short    uh_dport;        /* destination port */
    short    uh_ulen;        /* udp length */
    u_short    uh_sum;            /* udp checksum */
};

struct tcphdr 
{
    u_short    th_sport;        /* source port */
    u_short    th_dport;        /* destination port */
    tcp_seq    th_seq;            /* sequence number */
    tcp_seq    th_ack;            /* acknowledgement number */
#if BYTE_ORDER == LITTLE_ENDIAN
    u_char    th_x2:4,        /* (unused) */
              th_off:4;        /* data offset */
#endif
#if BYTE_ORDER == BIG_ENDIAN
    u_char    th_off:4,        /* data offset */
              th_x2:4;        /* (unused) */
#endif
    u_char    th_flags;
    u_short    th_win;            /* window */
    u_short    th_sum;            /* checksum */
    u_short    th_urp;            /* urgent pointer */
};
复制代码

更多网络协议格式细节参考:http://www.protocols.com/pbook/



    本文转自阿凡卢博客园博客,原文链接:http://www.cnblogs.com/luxiaoxun/archive/2012/08/08/2628627.html,如需转载请自行联系原作者

相关文章
|
26天前
|
域名解析 网络协议 关系型数据库
tcp和udp的区别是什么
TCP和UDP是互联网协议中的传输层协议。TCP是面向连接的,通过三次握手建立可靠连接,提供数据顺序和可靠性保证,适用于HTTP、FTP等需要保证数据完整性的应用。UDP则是无连接的,数据报独立发送,传输速度快但不保证可靠性,常用于实时通信、流媒体和DNS解析等对速度要求高的场景。根据应用需求选择合适的协议至关重要。
tcp和udp的区别是什么
|
30天前
|
网络协议 网络性能优化
认识TCP和UDP的区别
重排机制:由于UDP数据包可能因网络原因而发生乱序,因此在应用层需要对接收到的数据包进行排序。
25 4
|
1月前
|
网络协议 网络性能优化
网络面试题:TCP和UDP的区别
网络面试题:TCP和UDP的区别
25 0
|
1月前
|
网络协议 Python
Python网络编程实现TCP和UDP连接
Python网络编程实现TCP和UDP连接
27 0
|
1月前
|
网络协议 网络性能优化 Python
python怎么实现tcp和udp连接
python怎么实现tcp和udp连接
16 0
|
3月前
|
域名解析 网络协议
【计算机网络】DNS 分别在什么情况下使用 UDP 和 TCP?
【1月更文挑战第10天】【计算机网络】DNS 分别在什么情况下使用 UDP 和 TCP?
|
3月前
|
网络协议
【计算机网络】为什么DNS协议采用的是UDP作为传输层协议,而不是TCP?
【1月更文挑战第10天】【计算机网络】为什么DNS协议采用的是UDP作为传输层协议,而不是TCP?
|
3月前
|
网络协议 Linux 数据处理
网络编程【网络编程基本概念、 网络通信协议、IP地址 、 TCP协议和UDP协议】(一)-全面详解(学习总结---从入门到深化)
网络编程【网络编程基本概念、 网络通信协议、IP地址 、 TCP协议和UDP协议】(一)-全面详解(学习总结---从入门到深化)
79 3
|
1月前
|
网络协议 Linux
TCP 和 UDP 的 Socket 调用
【2月更文挑战第19天】
TCP 和 UDP 的 Socket 调用
|
1月前
|
网络协议 数据格式