乐闻世界logo
搜索文章和话题

What is the process and principle of TCP four-way wave?

2月21日 17:09

TCP Four-Way Wave Explained

TCP four-way wave is the process of terminating a connection, ensuring both parties can safely close the connection and release resources.

Wave Process

  1. First Wave (FIN): Client sends segment with FIN=1, seq=u, enters FIN_WAIT_1 state, indicating client has no more data to send
  2. Second Wave (ACK): Server receives FIN, sends segment with ACK=1, seq=v, ack=u+1, enters CLOSE_WAIT state, client enters FIN_WAIT_2 state after receiving
  3. Third Wave (FIN): Server sends segment with FIN=1, ACK=1, seq=w, ack=u+1, enters LAST_ACK state, indicating server also has no more data to send
  4. Fourth Wave (ACK): Client receives FIN, sends segment with ACK=1, seq=u+1, ack=w+1, enters TIME_WAIT state, server enters CLOSED state after receiving

Why Four-Way Wave is Necessary

  • TCP is a full-duplex protocol: Each direction must be closed separately
  • Second wave only confirms client's close request: Server may still have data to send to client, so cannot send FIN immediately
  • Third wave is server actively closing: Server sends FIN only after confirming no more data to send
  • TIME_WAIT state: Client needs to wait for 2MSL (Maximum Segment Lifetime) to ensure the final ACK can reach the server

Purpose of TIME_WAIT State

  • Ensure the final ACK reaches the server: If ACK is lost, server will retransmit FIN, client can resend ACK
  • Wait for all old segments to disappear: Ensure all old segments in the network have expired, avoiding impact on new connections
  • Why does TIME_WAIT state need to wait for 2MSL?
  • What happens if server closes before TIME_WAIT state?
  • What impact do large numbers of connections in TIME_WAIT state have on the server?
标签:TCP