zoukankan      html  css  js  c++  java
  • TCP三次握手抓包理解

      TCP建立连接需要三次握手,分手需要四次握手,平时在网上看到很多次,但是还没有很理解。为什么分手要多一次?可能是刚开始追求女生的时候比较容易,到分手的时候就比较麻烦了吧。。。

           了解某个东西要从它的基础开始,我们先看看TCP的报文是怎么回事。

           先看下tcp的报文结构,以下内容摘自官方文档,我简单的解释下,由于本人的英语水平主要是靠有道词典,如果解释错了,麻烦指出。。。

         

      

    Source Port. 16 bits.

    源端口,占16位

    Destination Port. 16 bits.

    目的端口,占16位

    Sequence Number. 32 bits.

    序列号,占32位
    The sequence number of the first data byte in this segment. If the SYN bit is set, the sequence number is the initial sequence number and the first data byte is initial sequence number + 1.

    序列号:随机生成一个序列号,如果SYN即同步序号状态设为1,此为当前连接的初始序列号,数据的第一个字节序号为此序列号+1。

    Acknowledgment Number. 32 bits.
    If the ACK bit is set, this field contains the value of the next sequence number the sender of the segment is expecting to receive. Once a connection is established this is always sent.

    确认号:确认序列号,如果ACK即确认序列号状态为1,则确认序列号=序列号+1,一旦连接建立,每次都会发送。

    Data Offset. 4 bits.
    The number of 32-bit words in the TCP header. This indicates where the data begins. The length of the TCP header is always a multiple of 32 bits.

    实际数据偏移量,指出数据的开始位置。

    reserved. 3 bits.
    Must be cleared to zero.

    3个位的保留位

    ECN, Explicit Congestion Notification. 3 bits.  
    Added in RFC 3168.

    000102
    N C E

    N, NS, Nonce Sum. 1 bit.
    Added in RFC 3540. This is an optional field added to ECN intended to protect against accidental or malicious concealment of marked packets from the TCP sender.

    C, CWR. 1 bit.

    E, ECE, ECN-Echo. 1 bit.

    Control Bits. 6 bits.

    000102030405
    U A P R S F

    U, URG. 1 bit.
    Urgent pointer valid flag.

    紧急标志位

    A, ACK. 1 bit.
    Acknowledgment number valid flag.

    确认标志位

    P, PSH. 1 bit.
    Push flag.

    推送标志位

    R, RST. 1 bit.
    Reset connection flag.

    重置连接标志位

    S, SYN. 1 bit.
    Synchronize sequence numbers flag.

    同步序列号标志位

    F, FIN. 1 bit.
    End of data flag.

    结束标志位

    Window. 16 bits, unsigned.
    The number of data bytes beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.

    滑动窗口,进行流量控制

    Checksum. 16 bits.
    This is computed as the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the TCP header, and the data, padded as needed with zero bytes at the end to make a multiple of two bytes. The pseudo header contains the following fields:

     

    校验和(没有细研究)

    Urgent Pointer. 16 bits, unsigned.
    If the URG bit is set, this field points to the sequence number of the last byte in a sequence of urgent data.

    紧急指针(没有细研究)

    Options. 0 to 40 bytes.
    Options occupy space at the end of the TCP header. All options are included in the checksum. An option may begin on any byte boundary. The TCP header must be padded with zeros to make the header length a multiple of 32 bits.

    可选项(没有细研究)
     Data. Variable length.

     下面我们来了解下传说中的三次握手和四次分:

    我们再用wireshark抓包验证下,这里是连Mysql前的三次握手

    1:客户端发起连接,设置SYN标志位为1,随机生成一个seq序列号x:

    像Wireshark这种工具,通常显示的都是相对序列号/确认号,而不是实际序列号/确认号

    如果想要关闭相对序列号/确认号,可以选择Wireshark菜单栏中的 Edit -> Preferences ->protocols->TCP,去掉Relative sequence number后面勾选框中的√即可

    参考:https://blog.csdn.net/a19881029/article/details/38091243

     2:服务器端收到信息后,设置SYN标志位为1,设置ACK标志位为1,随机生成一个seq序列号y,并生成确认号ack=seq(x)+1

     3:客户端返回信息,设置ACK标志位为1,生成确认号ack=seq(y)+1

     确认号都是在对方的序列号seq的基础上+1

     至此,三次握手完成了啦。

    下面我们再看一下,四次分手:

    1.客户端设置Sequence number和Acknowledgment number,发送FIN=1给服务器端,表示要关闭连接

    2. 服务器端返回Sequence number和Acknowledgment number,发送ACK=1给客户端,表示接收到客户端的请求

    3.服务器端返回Sequence number和Acknowledgment number,发送ACK=1,FIN=1给客户端表示可以关闭连接了

    4.客户端设置Sequence number和Acknowledgment number,发送ACK=1给服务器端,表示接收到服务端的信息。

      服务器收到客户端的信息后,关闭连接;客户端等待2MSL后依然没有收到回复,则证明Server端已正常关闭,所以客户端也关闭了连接。

    至此,四次分手圆满结束。

  • 相关阅读:
    pthread 的 api 分类
    移动端网页实现拨打电话功能的几种方法
    阿里云ECS服务器活动99元一年,最高可买三年
    jQuery 文档操作
    [Err] 1062
    中国标准城市区域码
    json和jsonp的使用区别
    xshell评估过期解决办法
    xshell评估期已过怎么办
    git之本地仓库关联远程仓库
  • 原文地址:https://www.cnblogs.com/zhanyd/p/9877762.html
Copyright © 2011-2022 走看看