TCP 协议理解

参考文档:http://tools.ietf.org/html/rfc793

3.1.  Header Format

Control Bits:  6 bits (from left to right):

   URG:  Urgent Pointer field significant

   ACK:  Acknowledgment field significant

   PSH:  Push Function

   RST:  Reset the connection

   SYN:  Synchronize sequence numbers

   FIN:  No more data from sender

扫描二维码关注公众号,回复: 652427 查看本文章

  0 1 2 3

    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Source Port          |       Destination Port        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                        Sequence Number                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Acknowledgment Number                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Data |           |U|A|P|R|S|F|                               |
   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
   |       |           |G|K|H|T|N|N|                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           Checksum            |         Urgent Pointer        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             data                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
用wireshark抓包
Flags: 0x10 (ACK) ==16,2的4次方,第五位
Flags: 0x18 (PSH, ACK) == 16+8,第五位,第4位
Flags: 0x12 (SYN, ACK)==16+2,第五位,第二位

3.2.  Terminology

TCB: Transmission Control Block,some variables being stored in a connection record.

Send Sequence Variables

      SND.UNA - send unacknowledged

      SND.NXT - send next

      SND.WND - send window

      SND.UP  - send urgent pointer

      SND.WL1 - segment sequence number used for last window update

      SND.WL2 - segment acknowledgment number used for last window

                update

      ISS     - initial send sequence number

    Receive Sequence Variables

      RCV.NXT - receive next

      RCV.WND - receive window

      RCV.UP  - receive urgent pointer

      IRS     - initial receive sequence number

Current Segment Variables

      SEG.SEQ - segment sequence number

      SEG.ACK - segment acknowledgment number

      SEG.LEN - segment length

      SEG.WND - segment window

      SEG.UP  - segment urgent pointer

      SEG.PRC - segment precedence value

9+1 states during its lifetime: 

LISTEN, SYN-SENT, SYN-RECEIVED,ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK,TIME-WAIT, and the fictional state CLOSED.

3.3.  Sequence Numbers

SND.UNA < SEG.ACK =< SND.NXT

RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND

and so on...

3.4.  Establishing a connection

1, the data must be buffered at the receiver until the connection reaches the ESTABLISHED state  

     TCP A                                                TCP B

  1.  CLOSED                                               LISTEN

  2.  SYN-SENT    --> <SEQ=100><CTL=SYN>               --> SYN-RECEIVED

  3.  ESTABLISHED <-- <SEQ=300><ACK=101><CTL=SYN,ACK>  <-- SYN-RECEIVED

  4.  ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK>       --> ESTABLISHED

  5.  ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK><DATA> --> ESTABLISHED

server每收到一个TCP数据包,都要回送一个ACK,接受失败的会[rst,ack]

client发送数据时,也要发送<PSH><ACK>(PSH的为data,ACK是上一次的)

2, Simultaneous initiation is only slightly more complex,双方都进行init connection,这种情况稍微复杂些。

3, Half-Open Connections 

4, re-Open Connections

3.5.  Closing a Connection

 The user who CLOSEs may continue to RECEIVE until he is told that the other side has CLOSED also

TCP A                                                TCP B

  1.  ESTABLISHED                                          ESTABLISHED

  2.  (Close)

      FIN-WAIT-1  --> <SEQ=100><ACK=300><CTL=FIN,ACK>  --> CLOSE-WAIT

  3.  FIN-WAIT-2  <-- <SEQ=300><ACK=101><CTL=ACK>      <-- CLOSE-WAIT

  4.                                                       (Close)

      TIME-WAIT   <-- <SEQ=300><ACK=101><CTL=FIN,ACK>  <-- LAST-ACK

  5.  TIME-WAIT   --> <SEQ=101><ACK=301><CTL=ACK>      --> CLOSED

  6.  (2 MSL)

      CLOSED

三次握手,c->s[SYN],s->c[SYN,ACK],c->s[ACK]

四次分手,c->s[FIN],s->c[ACK],s->c[FIN],c->s[ACK],因为server接收到fin时,还可以把未发送完的数据发送完,所以拆开了[FIN],[ACK]

猜你喜欢

转载自luckywnj.iteye.com/blog/1717717