TCP: terminating a connection 3

来源:互联网 发布:淘宝网男士真皮皮衣 编辑:程序博客网 时间:2024/06/17 22:01


TCP: terminating a connection

  • Terminating a TCP connection

    • Closing a TCP connection is amuch easiest than establishing a TCP connection(it is usually easier to destroy than to build....)

      The process is very much like ordinary data transfer - with thelast ACK step "dangling"


    • Properties of the disconnect operation:

        • Symmetric
        • Independent         



    • The TCP disconnect operation issymmetric

      In other words:

        • Both the client and server can initiate thedisconnect operation.

      (The connection request is always initialized by the client)



    • The TCP disconnect operation isindependent

      In other words:

        • It is possible for one side toclose its TCP connection without havingthe other end of the connectionto be closed...

        • The remaining TCP connection is auni-directional connection


    • Effect of closing a TCP connection:

        • The TCP module that closes its TCP connection will no longer be able totransmit

        • The TCP module that has closed its TCP connectioncan still continue to receive datafrom the other end !!!

          (Recall that a TCP connection is uni-directional)



    • Programming note:

        • The socket application programming interface (API)do not support a one-sided close operation....

          In a TCP socket, when one end of the TCP socket is closed, theother end of the TCP socket iscloses by the API automatically.


        • The Transport Layer Interface (TLI)fromAT&T does support one sided closing of aTCP end point...

          The function t_sndrel(int fd) willrelease the send capabilityof the TCP end.





  • Message exchanges in the disconnection operation of TCP

    • TCP close is also known as disconnect

    • The messages exchanged in theTCP disconnection protocol when theclient closes its connection --- is as follows:

        • Client sends a FIN request:

          The FIN message (the FIN bit in the TCP header is set) is used to disconnect aTCP connection


        • Server acknowledges theclient's FIN request:

          When the server TCP module received the FIN request, it does:

            • Send an ACK reply to the sender         

            • Invalid the ACK number


        • Client processes the ACK reply:

          When the sender of the FIN request received theACK reply, it invalidates its sequence number

          (Now the client cannot send message because its sequence number isinvalid)

      Note:

        • The reverse TCP connection isnot closed:

          The server has a valid sequence number !!!


        • We say that the TCP connection"half close".



        • (In the text book (if you bought one), theTCP state after closing one direction corresponds totheFIN_WAIT_2 state

          It is not the CLOSED state --- in the CLOSED state, both TCP connections areclosed).

        • While in the FIN_WAIT_2 state, theTCP module can still receive messages fromits peer...





  • Closing a "half open" TCP connection

    • At a later time, the other TCP module will also invoke aclose operation

      The TCP module will also senda FINISH request to its peer.

      There is a small difference in thisclose procedure...


    • Messages exchanged in closing the half open TCP connection (on behalf of the server):

        • Server sends FIN (disconnect) request:


        • The client acknowledges theFINISH request:

          NOTE:

            • The client sends an ACK for theFIN request

            • Then the client invalidates theACK number


            • Now the client's TCP state iscompletely empty !!!

              Now, you may be thinking that the TCP close operation at theclient is completed

              NO !!!, not yet...

              Reason:

                • The ACK message (for the FIN request can be lost:


                • The server would then time out andretransmit the FIN request:


                • This would lead to an abnormal termination of theTCP connection:



        • To minimize the likelihood of abnormal termination ofhalf open TCP connection, the client will:

            • wait 2 x MSL (Maximum Segment Livetime) beforeremoving the TCP state record


            • MSL is the maximum amount time that a packet can "live"in the Internet

              (due to routing errors, packets can be routingto very strange places in the Internet, but through the TTLfield, the lifetime of packets is limited.)

              We can't never be sure what MSL really is (depends on how congesteddifferent routers are...

                • MSL is set conservatively to 120 sec (2 min).

                  So the client will wait 4 min (2 × MSL) before removing theTCP state record         

          Example:

            • ACK is lost


            • Server retransmits theFIN request

              The client retransmits theACK...



        • Finally, when the server receives theACK, it deletes the TCP state record:

          Now the server does not have any record of thisTCP connection


        • After 2 × MSL delay, theclient will also delete the TCP state record:



    • Note:

        • There is still a slight chance that after240 sec (2 x MSL) of waiting, a retransmitted FIN request arrive.

          But such an event is rare



  • Simultaneous closing

    • Because each termination is indepedent of each other,it is possible that both TCP modulesinitiate theclose operation simultaneously.

    • The messages exchanged in such case are as follows:

      Notes:

        • Both TCP modules send a FIN request

        • Both TCP modules reply with anACK

          After sending the ACK,each TCP protocol modulemust wait2 x MSL (240 sec) before removing the TCP state record






0 0
原创粉丝点击