Why tcp connect termination need 4-way-handshake?

后端 未结 5 1250
感动是毒
感动是毒 2021-02-03 23:30

When connection sets up, there is:

Client ------SYN-----> Server

Client <---ACK/SYN---- Server ----①

Client ------ACK-----> Ser

相关标签:
5条回答
  • 2021-02-04 00:00

    I think of course the 2 and 3 can merge technically, but not flexible enough as not atomic.
    The first FIN1 C to S means and only means: I would close on my way of communication. The first ACK1 S to C means a response to FIN1. OK, I know your channel is disconnected but for my S(server) way connection, I'm not sure yet. Maybe my receiving buffer is not fully handled yet. The time I need is uncertain. Thus 2 and 3 are not appropriate to merge. Only the server would have right to decide when his way of connection can be disconnected.

    0 讨论(0)
  • 2021-02-04 00:10

    After googling a lot, I recognized that the four-way is actually two pairs of two-way handshakes.

    If termination is a REAL four-way actions, the 2 and 3 indeed can be set 1 at the same packet.

    But this a two-phase work: the first phase (i.e. the first two-way handshake) is :

    Client ------FIN-----> Server
    
    Client <-----ACK------ Server
    

    At this moment the client has been in FIN_WAIT_2 state waiting for a FIN from Server. As a bidirectional and full-duplex protocol, at present one direction has break down, no more data would be sent, but receiving still work, client has to wait for the other "half-duplex" to be terminated.

    While the FIN from the Server was sent to Client, then Client response a ACK to terminate the connection.

    Concluding note: the 2 and 3 can not merge into one package, because they belong to different states. But, if server has no more data or no data at all to be sent when received the FIN from client, it's ok to merge 2 and 3 in one package.

    References:

    1. http://www.tcpipguide.com/free/t_TCPConnectionTermination-2.htm
    2. http://www.tcpipguide.com/free/t_TCPConnectionEstablishmentProcessTheThreeWayHandsh-3.htm
    3. http://www.tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm
    0 讨论(0)
  • 2021-02-04 00:16

    If this is observed from the angle of coding, it is more reasonable to have 4 way than 3 way although both are possible ways for use.

    When a connection is to be terminated by one side, there are multiple possibilities or states that the peer may have. At least one is normal, one is on transmitting or receiving, one is in disconnected state somehow all of a sudden before this initiation.

    The way of termination should take at least above three into consideration for they all have high probabilities to happen in real life.

    So it becomes more natural to find out why based on above. If the peer is in offline state, then things are quite easy for client to infer the peer state by delving into the packet content captured in the procedure since the first ack msg could not be received from peer. But if the two messages are combined together, then it becomes much difficult for the client to know why the peer does not respond because not only offline state could lead to the packet missing but also the various exceptions in the procedure of processing in server side could make this happen. Not to mention the client needs to wait large amount of time until timeout. With the additional 1 message, the two problems could become more easier to handle from client side.

    The reason for it looks like coding because the information contained in the message is just like the log of code.

    0 讨论(0)
  • 2021-02-04 00:19

    From Wikipedia - "It is also possible to terminate the connection by a 3-way handshake, when host A sends a FIN and host B replies with a FIN & ACK (merely combines 2 steps into one) and host A replies with an ACK.[14]"

    Source:

    Wikipedia - https://en.wikipedia.org/wiki/Transmission_Control_Protocol

    [14] - Tanenbaum, Andrew S. (2003-03-17). Computer Networks (Fourth ed.). Prentice Hall. ISBN 0-13-066102-3.

    0 讨论(0)
  • 2021-02-04 00:24
    • In the Three-Way Handshake (connection setup) : 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.
      That why the server sends its SYN and the ACK of the client's SYN in a single segment.
    • In connection Termination : it takes four segments to terminate a connection since a FIN and an ACK are required in each direction.
      (2) means that The received FIN (first segment) is acknowledged (ACK) by TCP
      (3) means that sometime later the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.
      And then the last segment will mean that The TCP on the system that receives this final FIN acknowledges (ACK) the FIN.
    0 讨论(0)
提交回复
热议问题