Search code examples
tcpprotocolsnetwork-protocols

How TCP sequence numbers work in bidirectional communication/sliding window?


I was trying to understand how TCP works (not in detail of course). I was surfing the web for easy-to-understand flow diagrams and a question arose and couldn't find a straight answer.

Let's suppose that we have this nice flow where everything is sync (ignore the ending part with FIN and crossing packets):

enter image description here

I would like to know what would happen if both A and B would want to send packets at the same time (from what I understand TCP is a bidirectional protocol so each entity can send whenever it wants). Basically how the sequence numbers would behave. Having crossing data packets.

Also I read that TCP is a SWP (Sliding window protocol), I would like to know how ACKs are formed in this case. TCP is responding with number of bytes basically as Seq number, I can't really imagine how that can happen inside a window (where you can receive in any order). Is the implementation using two sets of seq numbers and two windows for both receiver and sender?

Any spec references or other helpful resources are more than welcome.


Solution

  • I would like to know what would happen if both A and B would want to send packets at the same time (from what I understand TCP is a bidirectional protocol so each entity can send whenever it wants). Basically how the sequence numbers would behave. Having crossing data packets.

    Each side in a TCP connection has its own sequence numbers. When side A sends bytes to side B, it increment its Sequence Number field, and side B increments its Acknowledgement field to signal which bytes were received. The opposite is true when side B sends data to side A - B's Sequence Number is incremented, and A's Acknowledgement. Both can happen simultaneously.

    Also I read that TCP is a SWP (Sliding window protocol), I would like to know how ACKs are formed in this case. TCP is responding with number of bytes basically as Seq number, I can't really imagine how that can happen inside a window (where you can receive in any order). Is the implementation using two sets of seq numbers and two windows for both receiver and sender?

    Yes. Both sides have their own independent sliding windows. Each side's send window is advanced according to the ACKs received from the other side, confirming the receipt of earlier sequence numbers.