Unix Network Programming Episode 13

本文深入探讨了TCP连接的建立与终止过程,包括三次握手和四次挥手的详细步骤,以及TCP状态转换图。文章还介绍了TCP选项,如MSS、窗口缩放和时间戳选项,以及它们在连接建立过程中的作用。
TCP Connection Establishment and Termination

To aid in our understanding of the connect, accept, and close functions and to help us debug TCP applications using the netstat program, we must understand how TCP connections are established and terminated, and TCP’s state transition diagram.

Three-Way Handshake

The following scenario occurs when a TCP connection is established:

1.The server must be prepared to accept an incoming connection. This is normally done by calling socket, bind, and listen and is called a passive open.
2.The client issues an active open by calling connect. This causes the client TCP to send a “synchronize” (SYN) segment, which tells the server the client’s initial sequence number for the data that the client will send on the connection. Normally, there is no data sent with the SYN; it just contains an IP header, a TCP header, and possible TCP options (which we will talk about shortly).
3.The server must acknowledge (ACK) the client’s SYN and the server must also send its own SYN containing the initial sequence number for the data that the server will send on the connection. The server sends its SYN and the ACK of the client’s SYN in a single segment.
4.The client must acknowledge the server’s SYN.

A and B
A: Hello, I want to communicate with you.
B: Sure, I am waiting for you.
A: Now, I am good prepared for it.
B: Let’s start.

The minimum number of packets required for this exchange is three; hence, this is called TCP’s three-way handshake.

TCP Options

Each SYN can contain TCP options. Commonly used options include the following:

  • MSS option.
  • Window scale option.
  • Timestamp option.
TCP Connection Termination

While it takes three segments to establish a connection, it takes four to terminate a connection.

1.One application calls close first, and we say that this end performs the active close. This end’s TCP sends a FIN segment, which means it is finished sending data.
2.The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an end-of-file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection.
3.Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.
4.The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.

Since a FIN and an ACK are required in each direction, four segments are normally required. We use the qualifier “normally” because in some scenarios, the FIN in Step 1 is sent with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive close and could be combined into one segment.

TCP State Transition Diagram

The operation of TCP with regard to connection establishment and connection termination can be specified with a state transition diagram.

Watching the Packets

The actual packet exchange that takes place for a complete TCP connection: the connection establishment, data transfer, and connection termination. We also show the TCP states through which each endpoint passes.

The client in this example announces an MSS of 536 (indicating that it implements only the minimum reassembly buffer size) and the server announces an MSS of 1,460 (typical for IPv4 on an Ethernet). It is okay for the MSS to be different in each direction.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值