Chapter 3-2 TCP.

Slides:



Advertisements
Similar presentations
智慧老伯的一席話 原稿 : 溫 Sir 中譯 : 老柳 A man of 92 years, short, very well- presented, who takes great care in his appearance, is moving into an old people’s.
Advertisements

5.9 TCP 的运输连接管理 1. 运输连接的三个阶段 运输连接就有三个阶段,即:连接建立、数据传送和连接释放。运输连接的管理就是使运输连接的建立和释放都能正常地进行。 连接建立过程中要解决以下三个问题: 要使每一方能够确知对方的存在。 要允许双方协商一些参数(如最大报文段长度,最大窗口大小,服务质量等)。
第五章 網際網路 5-1 網際網路的歷史沿革 5-2 網際網路基本運作原理 5-3 連線媒介與連線上網 5-4 網際網路上的熱門應用
Time Objectives By the end of this chapter, you will be able to
第 12 章 UDP 與 TCP.
Unit 9 Have you ever been to an amusement park? Section A.
第3章 网络安全协议基础.
Chapter 12 UDP 與 TCP.
第2章 计算机网络的协议与体系结构 2.1 计算机网络体系结构的形成 2.2 协议与划分层次 2.3 计算机网络的原理体系结构
第1章 概述.
第 7 章 运输层 基本内容 传输层的概念,TCP/IP体系中的传输层,端口的概念,用户数据报协议UDP,传输控制协议TCP,TCP报文格式、数据的编号与确认、流量控制、拥塞控制、重传机制、TCP的连接管理。 重点掌握 TCP/IP体系中的传输控制协议TCP:TCP报文格式、数据的编号与确认、流量控制、拥塞控制、重传机制、TCP的连接管理。
1. 理想的路由算法 有关路由选择协议的几个基本概念 算法必须是正确的和完整的。 算法在计算上应简单。
Author: Shigeki Takeuchi,Hiroyuki Koga, Katsuyoshi Iida,
Could you please clean your room?
Unit 4 I used to be afraid of the dark.
Lab312.
An Adaptive Cross-Layer Multi-Path Routing Protocol for Urban VANET
传输层是整个协议层次的核心,其任务是在源机器和目标机器之间提供可靠的、性价比合理的数据传输功能,并与当前所使用的物理网络完全独立
Advanced Sockets Programming
第 12 章 UDP 與 TCP.
Chapter 3 Transport Layer (傳輸層).
第3讲 网络安全协议基础 此为封面页,需列出课程编码、课程名称和课程开发室名称。
第五章 網際網路 5-1 網際網路的歷史沿革 5-2 網際網路基本運作原理 5-3 連線媒介與連線上網 5-4 網際網路上的熱門應用
TCP和UDP基本原理.
TCP报文格式.
TCP/UDP協定 10-1 TCP/UDP簡介 10-7 採用TCP或UDP 10-2 連接埠編號 10-8 UDP標頭格式
TCP/IP Protocol Suite TCP/IP協定 第二組 投影片製作by簡嘉宏 綦凱宏 林睿敏 滕孟哲.
第4章 网络互联与广域网 4.1 网络互联概述 4.2 网络互联设备 4.3 广域网 4.4 ISDN 4.5 DDN
Chapter 3 Transport Layer
Time Objectives By the end of this chapter, you will be able to
第十讲 TCP协议 协议概述 报文段格式 差错控制 流控和拥塞控制 TCP连接管理 TCP性能问题 TCP软件设计 2018/12/7
第4章 OSI傳輸層.
Remember the five simple rules to be happy 快樂的五個簡單常規
第4讲 传输层之二 本讲目的: 本讲概述: Internet传输层的实现和实例 面向连接的传输: TCP TCP拥塞控制 拥塞控制原则
The Wise Old Man 智慧老伯的一席話 原稿: 溫Sir 中譯 : 老柳 中譯潤稿:風刀雨箭
计算机网络(第 5 版) 第 5 章 传输层.
计算机网络 Computer Network
Chapter 12 傳輸控制通訊協定.
计算机网络 第 7 章 运输层 课件制作人:谢希仁.
實驗目的 明瞭可靠傳輸層的基礎觀念 TCP協定下區段資料傳送的格式
第七讲 网际协议IP.
NS2 – TCP/IP Simulation How-Wei Wu.
校園網路架構介紹與資源利用 主講人:趙志宏 圖書資訊館網路通訊組.
第5讲 网络层 本讲目的: 概述: 理解网络层服务原理: 因特网的实现实例 网络层的服务 路由选择原理 分层的路由选择 IP协议
客户服务 询盘惯例.
第 12 章 UDP 與 TCP 著作權所有 © 旗標出版股份有限公司.
第十三章 TCP/IP 與 Internet 網路連結技術
第7章 传输层协议——TCP与UDP 任课教师 卢豫开.
Westmont College 网络互连 Part 4 (传输协议, UDP and TCP, 协议端口)
Web Server 王宏瑾.
Remember the five simple rules to be happy 快樂的五個簡單常規
Mechanics Exercise Class Ⅰ
Lab312.
  传输控制协议 TCP TCP TCP 发送端 接收端 应用进程 应用进程 向发送缓存 写入数据块 从接收缓存 读取数据块 … …
中国科学技术大学计算机系 陈香兰 2013Fall 第七讲 存储器管理 中国科学技术大学计算机系 陈香兰 2013Fall.
Common Qs Regarding Earnings
The Wise Old Man 智慧老伯的一席話 原稿: 溫Sir 中譯 : 老柳
中央社新聞— <LTTC:台灣學生英語聽說提升 讀寫相對下降>
关联词 Writing.
True friendship is like sound health;
Remember the five simple rules to be happy 快樂的五個簡單常規
Remember the five simple rules to be happy 快樂的五個簡單常規
Distance Vector vs Link State
Chapter 10 Mobile IP TCP/IP Protocol Suite
Remember the five simple rules to be happy 快樂的五個簡單常規
动词不定式(6).
Distance Vector vs Link State Routing Protocols
The Wise Old Man 智慧老伯的一席話 原稿: 溫Sir 中譯 : 老柳
第7章 传输层协议——TCP与UDP 任课教师 卢豫开.
陳情表之外     with 三仁 三樂 歐陽宜璋製於 /10/23.
Presentation transcript:

Chapter 3-2 TCP

Chapter 3 outline 3.5 Connection-oriented transport: TCP segment structure reliable data transfer flow control connection management 3.6 Principles of congestion control 3.7 TCP congestion control 3.1 Transport-layer services 3.2 Multiplexing and demultiplexing 3.3 Connectionless transport: UDP 3.4 Principles of reliable data transfer Chapter 3-2 TCP 3-2 2

TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 point-to-point: one sender, one receiver reliable, in-order byte steam: no “message boundaries” pipelined: TCP congestion and flow control set window size send & receive buffers full duplex data: bi-directional data flow in same connection MSS: maximum segment size connection-oriented: handshaking (exchange of control msgs) init’s sender, receiver state before data exchange flow controlled: sender will not overwhelm receiver Chapter 3-2 TCP

TCP segment structure source port # dest port # application data 32 bits application data (variable length) sequence number acknowledgement number Receive window Urg data pnter checksum F S R P A U head len not used Options (variable length) URG: urgent data (generally not used) counting by bytes of data (not segments!) ACK: ACK # valid PSH: push data now (generally not used) # bytes rcvr willing to accept RST, SYN, FIN: connection estab (setup, teardown commands) Internet checksum (as in UDP) Chapter 3-2 TCP 3-4 4

simple telnet scenario TCP seq. #’s and ACKs Seq. #’s: byte stream “number” of first byte in segment’s data ACKs: seq # of next byte expected from other side cumulative ACK Q: how receiver handles out-of-order segments A: TCP spec doesn’t say, - up to implementor Host A Host B User types ‘C’ Seq=42, ACK=79, data = ‘C’ host ACKs receipt of ‘C’, echoes back ‘C’ Seq=79, ACK=43, data = ‘C’ host ACKs receipt of echoed ‘C’ Seq=43, ACK=80 time simple telnet scenario Chapter 3-2 TCP

TCP Round Trip Time and Timeout Q: how to set TCP timeout value? longer than RTT but RTT varies too short: premature timeout unnecessary retransmissions too long: slow reaction to segment loss Q: how to estimate RTT? SampleRTT: measured time from segment transmission until ACK receipt ignore retransmissions SampleRTT will vary, want estimated RTT “smoother” average several recent measurements, not just current SampleRTT Chapter 3-2 TCP 3-6 6

TCP Round Trip Time and Timeout EstimatedRTT = (1- )*EstimatedRTT + *SampleRTT Exponential weighted moving average influence of past sample decreases exponentially fast typical value:  = 0.125 Chapter 3-2 TCP 3-7 7

Example RTT estimation: Chapter 3-2 TCP 3-8 8

TCP Round Trip Time and Timeout Setting the timeout EstimtedRTT plus “safety margin” large variation in EstimatedRTT -> larger safety margin first estimate of how much SampleRTT deviates from EstimatedRTT: DevRTT = (1-)*DevRTT + *|SampleRTT-EstimatedRTT| (typically,  = 0.25) Then set timeout interval: TimeoutInterval = EstimatedRTT + 4*DevRTT Chapter 3-2 TCP 3-9 9

Chapter 3 outline 3.1 Transport-layer services 3.2 Multiplexing and demultiplexing 3.3 Connectionless transport: UDP 3.4 Principles of reliable data transfer 3.5 Connection-oriented transport: TCP segment structure reliable data transfer flow control connection management 3.6 Principles of congestion control 3.7 TCP congestion control Chapter 3-2 TCP 3-10 10

TCP reliable data transfer TCP creates rdt service on top of IP’s unreliable service Pipelined segments Cumulative acks TCP uses single retransmission timer Retransmissions are triggered by: timeout events duplicate acks Initially consider simplified TCP sender: ignore duplicate acks ignore flow control, congestion control Chapter 3-2 TCP 3-11 11

TCP sender events: data rcvd from app: Create segment with seq # seq # is byte-stream number of first data byte in segment start timer if not already running (think of timer as for oldest unacked segment) expiration interval: TimeOutInterval timeout: retransmit segment that caused timeout restart timer Ack rcvd: If acknowledges previously unacked segments update what is known to be acked start timer if there are outstanding segments Chapter 3-2 TCP

TCP sender (simplified) NextSeqNum = InitialSeqNum SendBase = InitialSeqNum loop (forever) { switch(event) event: data received from application above create TCP segment with sequence number NextSeqNum if (timer currently not running) start timer pass segment to IP NextSeqNum = NextSeqNum + length(data) event: timer timeout retransmit not-yet-acknowledged segment with smallest sequence number event: ACK received, with ACK field value of y if (y > SendBase) { SendBase = y if (there are currently not-yet-acknowledged segments) } } /* end of loop forever */ TCP sender (simplified) Comment: SendBase-1: last cumulatively ack’ed byte Example: SendBase-1 = 71; y= 73, so the rcvr wants 73+ ; y > SendBase, so that new data is acked Chapter 3-2 TCP 3-13 13

TCP: retransmission scenarios Host A Seq=92, 8 bytes data ACK=100 loss timeout lost ACK scenario Host B X time Host A Host B Seq=92 timeout Seq=92, 8 bytes data Seq=100, 20 bytes data ACK=100 ACK=120 Sendbase = 100 Seq=92, 8 bytes data SendBase = 120 Seq=92 timeout ACK=120 SendBase = 100 SendBase = 120 premature timeout time Chapter 3-2 TCP 3-14 14

TCP retransmission scenarios (more) Host A Seq=92, 8 bytes data ACK=100 loss timeout Cumulative ACK scenario Host B X Seq=100, 20 bytes data ACK=120 time SendBase = 120 Chapter 3-2 TCP 3-15 15

TCP ACK generation [RFC 1122, RFC 2581] Event at Receiver Arrival of in-order segment with expected seq #. All data up to expected seq # already ACKed expected seq #. One other segment has ACK pending Arrival of out-of-order segment higher-than-expect seq. # . Gap detected Arrival of segment that partially or completely fills gap TCP Receiver action Delayed ACK. Wait up to 500ms for next segment. If no next segment, send ACK Immediately send single cumulative ACK, ACKing both in-order segments Immediately send duplicate ACK, indicating seq. # of next expected byte Immediate send ACK, provided that segment starts at lower end of gap Chapter 3-2 TCP 3-16 16

Fast Retransmit Time-out period often relatively long: long delay before resending lost packet Detect lost segments via duplicate ACKs. Sender often sends many segments back-to-back If segment is lost, there will likely be many duplicate ACKs. If sender receives 3 ACKs for the same data, it supposes that segment after ACKed data was lost: fast retransmit: resend segment before timer expires Chapter 3-2 TCP 3-17 17

X time Figure 3.37 Resending a segment after triple duplicate ACK Host A timeout Host B time X resend 2nd segment Figure 3.37 Resending a segment after triple duplicate ACK Chapter 3-2 TCP 3-18 18

Fast retransmit algorithm: event: ACK received, with ACK field value of y if (y > SendBase) { SendBase = y if (there are currently not-yet-acknowledged segments) start timer } else { increment count of dup ACKs received for y if (count of dup ACKs received for y = 3) { resend segment with sequence number y a duplicate ACK for already ACKed segment fast retransmit Chapter 3-2 TCP Transport Layer 3-19 19

Chapter 3 outline 3.5 Connection-oriented transport: TCP segment structure reliable data transfer flow control connection management 3.6 Principles of congestion control 3.7 TCP congestion control 3.1 Transport-layer services 3.2 Multiplexing and demultiplexing 3.3 Connectionless transport: UDP 3.4 Principles of reliable data transfer Chapter 3-2 TCP Transport Layer 3-20 20

TCP Flow Control receive side of TCP connection has a receive buffer: sender won’t overflow receiver’s buffer by transmitting too much, too fast flow control receive side of TCP connection has a receive buffer: speed-matching service: matching the send rate to the receiving app’s drain rate app process may be slow at reading from buffer Chapter 3-2 TCP Transport Layer 3-21 21

TCP Flow control: how it works Rcvr advertises spare room by including value of RcvWindow in segments Sender limits unACKed data to RcvWindow guarantees receive buffer doesn’t overflow (Suppose TCP receiver discards out-of-order segments) spare room in buffer = RcvWindow = RcvBuffer-[LastByteRcvd - LastByteRead] Chapter 3-2 TCP Transport Layer 3-22 22

Chapter 3 outline 3.1 Transport-layer services 3.2 Multiplexing and demultiplexing 3.3 Connectionless transport: UDP 3.4 Principles of reliable data transfer 3.5 Connection-oriented transport: TCP segment structure reliable data transfer flow control connection management 3.6 Principles of congestion control 3.7 TCP congestion control Chapter 3-2 TCP Transport Layer 3-23 23

Connection establishment and release Chapter 3-2 TCP

Connection Establishment Basic idea: To establish a connection, you send off a connection request to the other end. The other end then accepts the connection, and returns an acknowledgment. Big problem: Suppose you don’t get an answer, so you do another request. Your first request didn’t make it: no harm done. The ack didn’t make it back: you’re establishing a second connection, but this can probably be detected. Your first request didn’t make it yet: now you’re really making a second connection and no one knows you didn’t do this on purpose. Chapter 3-2 TCP

Connection Establishment Problem Main cause: The network has storage capabilities, and unpredictable delays. This means that things can pop up out of the blue. Attacking Duplicates Solution: Restrict the lifetime of TPDUs – if the maximum lifetime is known in advance, we can be sure that a previous packet is discarded and that it won’t interfere with successive ones. Basic idea: Assign sequence numbers to TPDUs, and let the sequence number space be so large that no two outstanding TPDUs can have the same number. Chapter 3-2 TCP

Connection Establishment Problem Problem: When a host crashes, it has to start numbering TPDUs again. So, where does it start? You can’t just wait the maximum packet lifetime T and start counting from the start again: in wide area systems, T may be too large to do that. The point is that you must avoid that an initial sequence number corresponds to a TPDU still floating around. So, just find the right initial number. Solution: Assign sequence numbers in accordance to clock ticks, and assume that the clock continues ticking during a crash. This leads to a forbidden region: Chapter 3-2 TCP

Linear relation between time and initial sequence numbers (a) TPDUs may not enter forbidden reg. (b) resynchronization problem Every time you want to assign a next sequence number, check whether that number is in the forbidden region. Watch it: when sequence numbers are assigned at a lower pace than the clock ticks, we may enter the region “from the top.” Likewise, assigning them too fast makes you enter the region “from the bottom.” Chapter 3-2 TCP

Error-Free Connection Establishment Problem: Great, we have a way of avoiding duplicates, but how do we get a connection in the first place? Note: One way or the other we have to get the sender and receiver to agree on initial sequence numbers. We need to avoid that an old (unnumbered) connection request pops up. Solution: Three-way handshake. Chapter 3-2 TCP

TCP Connection Management Three way handshake: Step 1: client host sends TCP SYN segment to server specifies initial seq # no data Step 2: server host receives SYN, replies with SYNACK segment server allocates buffers specifies server initial seq. # Step 3: client receives SYNACK, replies with ACK segment, which may contain data Recall: TCP sender, receiver establish “connection” before exchanging data segments initialize TCP variables: seq. #s buffers, flow control info (e.g. RcvWindow) client: connection initiator Socket clientSocket = new Socket("hostname","port number"); server: contacted by client Socket connectionSocket = welcomeSocket.accept(); Chapter 3-2 TCP Transport Layer 3-30 30

TCP connection acknowledgment host A host B client server SYN, SEQ = x request SYN, ACK, SEQ = y, ACK= x  1 acknowledgment ACK, SEQ = x + 1, ACK = y  1 acknowledgment Chapter 3-2 TCP

Three-way Handshake Chapter 3-2 TCP

Connection Release Asymmetric release: one party just closes down the connection. May result in loss of data: Chapter 3-2 TCP

Symmetric Connection Release Practical solution: Use timeout mechanisms. This will catch most cases, but it is never a fool-proof solution: the initial DR and all retransmissions may still be lost, resulting in a half-open connection. The two-army problem. Chapter 3-2 TCP

Chapter 3-2 TCP

TCP Connection Management (cont.) Closing a connection: client closes socket: clientSocket.close(); Step 1: client end system sends TCP FIN control segment to server Step 2: server receives FIN, replies with ACK. Closes connection, sends FIN. client FIN server ACK close closed timed wait Chapter 3-2 TCP

TCP Connection Management (cont.) Step 3: client receives FIN, replies with ACK. Enters “timed wait” - will respond with ACK to received FINs Step 4: server, receives ACK. Connection closed. Note: with small modification, can handle simultaneous FINs. client server closing FIN ACK closing FIN ACK timed wait closed closed Chapter 3-2 TCP Transport Layer 3-37 37

TCP Connection Management (cont) TCP server lifecycle TCP client lifecycle Chapter 3-2 TCP Transport Layer 3-38 38

TCP transition process SYN, SEQ = x Client process Server process LISTEN(open) (open) SYN_SENT SYN_RCVD ESTABLISHED CLOSE_WAIT FIN_WAIT_2 LAST_ACK TIME_WAIT CLOSED (duplex data transition) SYN, ACK, SEQ = y, ACK = x + 1 ACK, SEQ = x + 1, ACK = y + 1 FIN, SEQ = u ACK, SEQ = v, ACK = u + 1 FIN, ACK, SEQ = v, ACK = u + 1 ACK, SEQ = u + 1, ACK = v + 1 Chapter 3-2 TCP

Next Appendix 3.6 Principles of congestion control 3.7 TCP congestion control Chapter 3-2 TCP

两次握手 B给A发送一个连接请求分组,A收到了这个分组,并发送了确认应答分组。按照两次握手的协定,A认为连接已经成功地建立了,可以开始发送数据分组。 B在A的应答分组在传输中被丢失的情况下,将不知道A是否已准备好,不知道A建议什么样的序列号,B甚至怀疑A是否收到自己的连接请求分组。 在这种情况下,B认为连接还未建立成功,将忽略A发来的任何数据分组,只等待连接确认应答分组。而A在发出的分组超时后,重复发送同样的分组。这样就形成了死锁。 Chapter 3-2 TCP

建立 TCP 连接 A 的 TCP 向 B 发出连接请求报文段,其首部中的同步比特 SYN 应置为 1,并选择序号 x,表明传送数据时的第一个数据字节的序号是 x。 B 的 TCP 收到连接请求报文段后,如同意,则发回确认。 B 在确认报文段中应将 SYN 置为 1,其确认号应为 x  1,同时也为自己选择序号 y。 A 收到此报文段后,向 B 给出确认,其确认号应为 y  1。 A 的 TCP 通知上层应用进程,连接已经建立。 当运行服务器进程的主机 B 的 TCP 收到主机 A 的确认后,也通知其上层应用进程,连接已经建立。 Chapter 3-2 TCP

TCP 的数据编号与确认 TCP 协议是面向字节的。TCP 将所要传送的报文看成是字节组成的数据流,并使每一个字节对应于一个序号。 Chapter 3-2 TCP

TCP seq. #’s 和 ACKs Seq. #’s: 该数据段第一个字节在(整个报文)字节流中 “编号” ACKs: A: TCP 没有定义, - 由程序设计者决定 Host A Host B User types ‘C’ Seq=42, ACK=79, data = ‘C’ host ACKs receipt of ‘C’, echoes back ‘C’ Seq=79, ACK=43, data = ‘C’ host ACKs receipt of echoed ‘C’ Seq=43, ACK=80 time 简单的 telnet 场景 Chapter 3-2 TCP

32 bit TCP 报文段 发送在前 IP 首部 IP 数据部分 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节的 固定首部 TCP 首部 确 认 号 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 TCP 报文段 TCP 首部 TCP 数据部分 发送在前 IP 首部 IP 数据部分 Chapter 3-2 TCP

源端口和目的端口字段——各占 2 字节。端口是传输层与应用层的服务接口。传输层的复用和分用功能都要通过端口才能实现。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 源端口和目的端口字段——各占 2 字节。端口是传输层与应用层的服务接口。传输层的复用和分用功能都要通过端口才能实现。 Chapter 3-2 TCP

比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 序号字段——占 4 字节。TCP 连接中传送的数据流中的每一个字节都编上一个序号。序号字段的值则指的是本报文段所发送的数据的第一个字节的序号。 Chapter 3-2 TCP

确认号字段——占 4 字节,是期望收到对方的下一个报文段的数据的第一个字节的序号。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 确认号字段——占 4 字节,是期望收到对方的下一个报文段的数据的第一个字节的序号。 Chapter 3-2 TCP

数据偏移(首部长度)——占 4 bit,它指出 TCP 报文段的数据起始处距离 TCP 报文段的起始处有多远。“数据偏移”的单位是4 字节。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 数据偏移(首部长度)——占 4 bit,它指出 TCP 报文段的数据起始处距离 TCP 报文段的起始处有多远。“数据偏移”的单位是4 字节。 Chapter 3-2 TCP

保留字段——占 6 bit,保留为今后使用,但目前应置为 0。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 保留字段——占 6 bit,保留为今后使用,但目前应置为 0。 Chapter 3-2 TCP

紧急比特 URG —— 当 URG  1 时,表明紧急指针字段有效。它告诉系统此报文段中有紧急数据,应尽快传送(相当于高优先级的数据)。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 紧急比特 URG —— 当 URG  1 时,表明紧急指针字段有效。它告诉系统此报文段中有紧急数据,应尽快传送(相当于高优先级的数据)。 Chapter 3-2 TCP

确认比特 ACK —— 只有当 ACK  1 时确认号字段才有效。当 ACK  0 时,确认号无效。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 确认比特 ACK —— 只有当 ACK  1 时确认号字段才有效。当 ACK  0 时,确认号无效。 Chapter 3-2 TCP

比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 推送比特 PSH (PuSH) —— 接收 TCP 收到推送比特置 1 的报文段,就尽快地交付给接收应用进程,而不再等到整个缓存都填满了后再向上交付。 Chapter 3-2 TCP

比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 复位比特 RST (ReSeT) —— 当 RST  1 时,表明 TCP 连接中出现严重差错(如由于主机崩溃或其他原因),必须释放连接,然后再重新建立传输连接。 Chapter 3-2 TCP

同步比特 SYN —— 同步比特 SYN 置为 1,就表示这是一个连接请求或连接接受报文。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 同步比特 SYN —— 同步比特 SYN 置为 1,就表示这是一个连接请求或连接接受报文。 Chapter 3-2 TCP

终止比特 FIN (FINal) —— 用来释放一个连接。当FIN  1 时,表明此报文段的发送端的数据已发送完毕,并要求释放传输连接。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 终止比特 FIN (FINal) —— 用来释放一个连接。当FIN  1 时,表明此报文段的发送端的数据已发送完毕,并要求释放传输连接。 Chapter 3-2 TCP

比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 窗口字段 —— 占 2 字节。窗口字段用来控制对方发送的数据量,单位为字节。TCP 连接的一端根据设置的缓存空间大小确定自己的接收窗口大小,然后通知对方以确定对方的发送窗口的上限。 Chapter 3-2 TCP

检验和 —— 占 2 字节。检验和字段检验的范围包括首部和数据这两部分。在计算检验和时,要在 TCP 报文段的前面加上 12 字节的伪首部。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 检验和 —— 占 2 字节。检验和字段检验的范围包括首部和数据这两部分。在计算检验和时,要在 TCP 报文段的前面加上 12 字节的伪首部。 Chapter 3-2 TCP

紧急指针字段 —— 占 16 bit。紧急指针指出在本报文段中的紧急数据的最后一个字节的序号。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 紧急指针字段 —— 占 16 bit。紧急指针指出在本报文段中的紧急数据的最后一个字节的序号。 Chapter 3-2 TCP

MSS 是 TCP 报文段中的数据字段的最大长度。 比特 0 8 16 24 31 源 端 口 目 的 端 口 MSS 是 TCP 报文段中的数据字段的最大长度。 数据字段加上 TCP 首部 才等于整个的 TCP 报文段。 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 选项字段 —— 长度可变。TCP 只规定了一种选项,即最大报文段长度 MSS (Maximum Segment Size)。MSS 告诉对方 TCP:“我的缓存所能接收的报文段的数据字段的最大长度是 MSS 个字节。” Chapter 3-2 TCP

填充字段 —— 这是为了使整个首部长度是 4 字节的整数倍。 比特 0 8 16 24 31 源 端 口 目 的 端 口 序 号 20 字节 固定 首部 确 认 号 TCP 首部 数据 偏移 U R G A C K P S H R S T S Y N F I N 保 留 窗 口 检 验 和 紧 急 指 针 选 项 (长 度 可 变) 填 充 填充字段 —— 这是为了使整个首部长度是 4 字节的整数倍。 Chapter 3-2 TCP

从 A 到 B 的连接就释放了,连接处于半关闭状态。 至此,整个连接已经全部释放。 TCP 连接释放的过程 主机 A 主机 B 应用进程 释放连接 B 不再发送报文 ② 通知主机 应用进程 ① 应用进程 释放连接 A 不再发送报文 FIN, SEQ = x ACK, SEQ = y, ACK= x  1 确认 从 A 到 B 的连接就释放了,连接处于半关闭状态。 相当于 A 向 B 说: “我已经没有数据要发送了。 但你如果还发送数据,我仍接收。” FIN, ACK, SEQ = y, ACK = x + 1 ACK, SEQ = x + 1, ACK = y  1 确认 Chapter 3-2 TCP

TCP 的有限状态机 为了管理因特网,在网络管理中心设有管理 信息库 MIB (Management Information Base)。 TCP 连接表对每个连接都登记了其连接信息。除本地和远地的 IP 地址和端口号外,还要记录每一个连接所处的状态。 连接状态 本地 IP 地址 本地端口 远地 IP 地址 远地端口 连接 1 连接 2 连接 n Chapter 3-2 TCP

TCP 的 有 限 状 态 机 起点 CLOSED 被动打开 主动打开 发送 SYN 关闭 收到 SYN 发送 SYN, ACK LISTEN TCP 的 有 限 状 态 机 被动打开 收到 RST 发送 SYN 关闭 或超时 收到 SYN,发送 SYN, ACK SYN_RCVD SYN_SENT 同时打开 主动打开 收到 ACK 收到 SYN, ACK 发送 ACK 数据传送 阶段 收到 FIN 发送 ACK 被动关闭 关闭 发送 FIN ESTABLISHED CLOSE_WAIT 关闭 发送 FIN 主动关闭 关闭 发送 FIN 收到 FIN 发送 ACK 同时关闭 FIN_WAIT_1 CLOSING 收到 ACK 收到 FIN, ACK 发送 ACK LAST_ACK 收到 ACK 收到 ACK 收到 FIN 发送 ACK Chapter 3-2 TCP FIN_WAIT_2 TIME_WAIT 定时经过两倍报文段寿命后