When using TCP, devices must first establish a connection with a peer system
before data transmission can take place
so connection orientated session will be established between host A and host B
One machine will initiate the connection which must then the accepted by the other machine.
Protocols software modules in the operating systems of the host devices
communicate with each other by sending messages across the network
to verify that the transfer is authorized
and that both sides are ready for the transmission of data.
For this to take place a three-way handshake occurs between the host devices using TCP.
So host initiating the session will set the SYN flag or SYN bit
in the TCP header of the initial segment send to host B.
Host A will also choose an initial sequence number
which in this example let's say 100.
So the control flag SYN is set on
and the sequence number is set to an initial value of 100.
This is then used to start the handshake process.
This synchronization segment also specifies the number of the port
to which the sender wants to connect for instance port 80 or HTTP.
The host on the right hand side will be waiting for a connection request
from the remote client, in this case host A.
when the SYN is received and accepted
host b will send back a TCP segment with both the SYN and ACK flags set.
So the control flags SYN and ACK are set on
and use to negotiate the connection
and acknowledge receipt of the initials synchronization segment of the sender.
B also set an initial sequence number
to indicate the next sequence number of the next byte of data
it expects to receive from host A.
host B also sets the acknowledgement flag to, in this case 101
an ACK flag indicates, the next portion of data, the host expects to receive.
So host A initially sent a sequence number of a 100
and host B, in this case were assuming a window size of 1
sends back an acknowledgement of a 101.
The third step in the three-way handshake is where the initiating host
in this case host A has received the SYN from host B and sends back a TCP segment
with the control field set to ACK, in other words acknowledgement.
Host A is therefore acknowledging the next segment it expect to receive from host B
in this case 301, initially host B sent a sequence number of 300.
So host A is expecting the next segment 301.
Host A sets it's sequence number to 101.
The initial segment sent was 100 and the next one in this case is 101
because the SYN bit or SYN flag is unset
this confirms that three-way handshake has completed successfully.
Just to reiterate, the control bits or flags initially A
sends a segment with the SYN bit or SYN flag set on.
So the control flag SYN is set to 1.
Host B in the second step of the three-way handshake
sets its control flags or bits to SYN ACK.
In other words the SYN bit is set to 1
and the ACK bit is set to 1.
In the last step of the three-way handshake
Host A sets the ACK bit to 1 or the ACK flag is set on.
The SYN bit or SYN flag is set to 0 to indicate
that the three-way handshake has completed successfully.
Now sequence numbers and acknowledgements can cause a lot confusion
So I'm going to explain them in more detail now.
We're assuming in this example, that a window size of 1 is used.
Now if you remember the window size is the maximum amount of data
that the receiver can receive from a sender and process correctly.
So we're going to assume that only 1 segment can be transmitted
at any given time before an acknowledgement is received
to acknowledge receipt of that segment.
So let’s assume A starts with an initial sequence number of 5
because of a window size of 1, only 1 segment can be sent from A to B.
B successfully receives the segment from A
and acknowledges the next segment that it wants to receive.
so rather than acknowledging receipt of sequence number of 5
it acknowledges for sequence number 6
which implies that all previous sequence numbers will receive correctly.
So B acknowledges in this case for sequence number 6
but B may start with an initial sequence number of 10.
so in the TCP header B tells A that its initial sequence number is 10
and that its successfully received sequence number 5 from A
and is expecting sequence number 6 from A in the next packet.
Let’s assume successfully receives that segment
so A will now send segment 6 to B
in other words the next sequence number.
A also acknowledges receipt of segment 10 from host B.
So A has successfully received the segment with sequence number 10.
Please note once again that the host
can randomly choose the initial sequence numbers
and thus in the initial three-way handshake
that information needs to be communicated between the 2 hosts
so that they know what the initial sequence numbers are.
So once again A sends sequence number 6 to B and acknowledges sequence 11.
Let’s assume B successfully receives that segment
and therefore will acknowledge for segment 7.
The next segment it expects to receive
once again 7 in the acknowledgement indicates that previous segment was successfully received.
So host B by setting the sequence number to 7 is telling host A
that sequence number 6 was successfully received.
Host B sends sequence number 11
because that’s the next sequence number that A expects to receive.
The thing to note once again, is that initial sequence numbers
that the host expects to receive and the sequence of number of for instance 11
implies that sequence number 10 and previous sequence number was successfully received.
Now once again flow control prevents an issue
where the sender is sending so much data
that the buffers of the receiver are overflowed.
If this is a very powerful machine and this is an older machine
that is not as powerful, it’s possible that A can overrun
the buffers of B because it’s sending so much data.
So B needs a mechanism to tell A to slowdown
so that D can successfully process the traffic that it’s receiving.
So as an example let’s assume
that the window size in this example is 3 rather than 1.
So A can send 3 segments of data before getting an acknowledgement.
The advantage of increasing the window size is that throughput
can increase dramatically because a host can send more data
with fewer acknowledgements and therefore the round trip timers decrease dramatically.
So in this example A sends 3 segments to B.
Let’s assume that the received buffer of B is full
and it can't handle that amount of data
B will send a not ready indicator to A
and it does this by setting the windows size to 0.
This tells the sender to stop sending data
and wait for ready indicator from the receiver.
Assuming that host B has now been able to process the data
in it’s receive buffer and can now receive more data.
It can send a ready indicator to A, to tell it to resume sending of datagrams.
so A resumes the transmission by sending for instance 3 segments to B.
Because the window size is 3.
So please note in the background the TCP host involve in the conversation
can negotiate various parameters and one of them is flow control
where a receiver, can tell a sender to slowdown or to stop sending data
until the receiver has buffer space available to receive transmitted segments.
This is allows for communication between a very powerful
or fast machine and a slower or less powerful machine
where they can negotiate the rate of transmission.