TheHingineer

  • Computer Networks


  • CN Part-1

  • CN Part-2

  • CN Part-3

  • CN Part-4

  • CN Part-5

  •  TCP Segment Format and Connection Establishment (3-Way Handshake) in Computer Networks

     TCP kya hai? (Transmission Control Protocol)

    • TCP (Transmission Control Protocol) Transport Layer ka sabse important protocol hai, jo unreliable IP network ke upar 100% Reliable, Connection-Oriented, aur Byte-Stream data delivery provide karta hai.

    • Jab aap koi file download karte hain ya web page open karte hain, toh ek single bit bhi corrupt ya missing nahi honi chahiye. Isliye web traffic (HTTP/HTTPS), file transfer (FTP), aur emails (SMTP) sabhi TCP par rely karte hain.

    • TCP communication shuru karne se pehle dono endpoints ke beech formal connection establish karta hai jise Three-Way Handshake kehte hain.

     1. TCP Segment Format

    TCP header ka minimum size 20 Bytes aur options ke saath maximum 60 Bytes hota hai:

     0                   1                   2                   3
     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |          Source Port          |       Destination Port        |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                        Sequence Number                        |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                    Acknowledgment Number                      |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |  Data |           |U|A|P|R|S|F|                               |
    | Offset| Reserved  |R|C|S|S|Y|I|            Window Size        |
    |       |           |G|K|H|T|N|N|                               |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |           Checksum            |         Urgent Pointer        |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                    Options (0 to 40 Bytes)                    |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    Field Size Purpose aur Explanation
    Source / Dest Port 16 bits each Sending aur Receiving applications ke Port numbers.
    Sequence Number 32 bits Segment ke pehle data byte ka sequential number. Data reassembly aur in-order delivery ke liye use hota hai.
    Acknowledgment Number 32 bits Next expected byte number (Cumulative ACK). Agar ACK = 501 hai, matlab receiver ne byte 500 tak sab kuch successfully receive kar liya hai aur ab byte 501 ka wait kar raha hai.
    Data Offset (Header Length) 4 bits TCP header ka size 32-bit words mein. Minimum 5 (5 * 4 = 20 Bytes), Maximum 15 (60 Bytes).
    Control Flags 6 bits – URG: Urgent pointer field valid hai
    – ACK: Acknowledgment field valid hai
    – PSH: Push data immediately to application (no buffering)
    – RST: Reset connection immediately
    – SYN: Synchronize sequence numbers (Connection setup)
    – FIN: Terminate connection (No more data from sender)
    Window Size 16 bits Flow control ke liye. Receiver batata hai ki uske buffer mein kitne bytes receive karne ki jagah baaki hai (Receiver Advertised Window – rwnd).
    Checksum 16 bits TCP header, data, aur pseudo-IP header ki error checking ke liye mandatory checksum.

     2. TCP Connection Establishment: 3-Way Handshake

    TCP connection establish karne ke liye Client aur Server ke beech 3 messages exchange hote hain:

    Client (Host A)                                                 Server (Host B)
      |                                                                    |
      | (LISTEN state)                                                     | (LISTEN state)
      |                                                                    |
      |============== Step 1: SYN (Seq = x) ==============================>| (SYN_RCVD state)
      |   Client: "Hi, let's connect! My initial seq number is x"          |
      |                                                                    |
      |<============= Step 2: SYN + ACK (Seq = y, Ack = x + 1) ============|
      |   Server: "Got your SYN! My seq is y, and I expect x+1 from you"   |
      |                                                                    |
      | (ESTABLISHED state)                                                |
      |                                                                    |
      |============== Step 3: ACK (Ack = y + 1) ==========================>| (ESTABLISHED state)
      |   Client: "Got your SYN! I expect y+1 from you. Connection open!"   |
      |                                                                    |
      <==================== Full-Duplex Data Transfer Begins ==============>

    Step 1 (SYN): Client server ko ek segment bhejta hai jisme SYN = 1 flag set hota hai aur ek random initial sequence number Seq = x choose karta hai.
    Step 2 (SYN-ACK): Server request accept karke reply karta hai: SYN = 1, ACK = 1, Ack = x + 1, aur apna initial sequence number Seq = y set karta hai.
    Step 3 (ACK): Client server ke SYN ko acknowledge karta hai: ACK = 1, Ack = y + 1. Ab connection fully establish ho chuka hai aur dono sides data transmit kar sakti hain.

     3. TCP Connection Termination (4-Way Handshake)

    Kyunki TCP Full-Duplex hota hai, isliye connection band karne ke liye dono directions ko alag-alag close karna padta hai:

    1. Client bhejta hai: FIN (Client: “Maine apna data bhej liya, ab main kuch nahi bhejunga”).
    2. Server reply karta hai: ACK (Server: “Got your FIN, client-to-server direction closed”).
    3. Server apna remaining data khatam karke bhejta hai: FIN (Server: “Mera data bhi khatam ho gaya”).
    4. Client reply karta hai: ACK (Client: “Got your FIN”). Client TIME_WAIT duration (2 * MSL) wait karta hai aur connection gracefully close ho jaata hai.

     Summary Table

    TCP Phase Summary
    Header Size 20 Bytes minimum to 60 Bytes maximum.
    Connection Setup 3-Way Handshake (SYN → SYN-ACK → ACK).
    Connection Termination 4-Way Handshake (FIN → ACK → FIN → ACK).
    Scroll to Top