you've used TCP many times and probably not even known it it's the feature-rich transport
protocol and the one that forms connections is time to see how these connections work PCP is
connection oriented if you're attempting an exam you may hear the phrase connection establishment
and termination this means a device will start a connection to another device they'll agree to
form a connection and set up parameters like port and sequence numbers don't worry we'll cover all
of these soon this is in contrast to UDP which is connectionless the application or operating
system may do something to track the data going in and out but as far as the network stack is
concerned the devices just start sending data but don't worry about that for now we're here for TCP
the connection establishment phase is called a three-way handshake you'll see why it gets
that name soon to do this it uses flags in the TCP header flags are bits that may be turned on
or off the device starting the conversation that is the client sends a segment to the server there
is no payload in this segment just headers in the TCP header the syn flag is turned on sin is short
for synchronized this tells the server that we want to start a new connection and we need to
agree on a few details the first of these details are the source and destination ports that we want
to use as we've talked about before the source port will be random and the destination will be
a well known number the initial sequence number or is n is also set this helps both devices keep
track of which all of the segment's should be processed in this usually starts as a randomized
value oh I'm getting too detail about this but that's usually done for security a window value
is also set but you can ignore that for now as we'll cover that in detail in the next video
now that the server has the first message it can decide if it wants to be in this verse Asian if
it has an application listening on port 80 then it will indeed agree to this conversation so it
will respond by sending an empty TCP segment of its own in the TCP header the source and
destination ports will switch the sequence number will go up by one as is the next message in the
conversation and finally the syn and AK Flags are both set ack is short for acknowledged by
setting both of these flags the server is saying I have seen your request for a conversation and I
agree to it so that's two parts done we've got one more to go the client sends an empty TCP segment
with the ACK fieldset and the sequence number is incremented again this is the client confirming
that it received the service last message the syn synack and ack messages are the three-way
handshake that TCP uses to build a connection at this point the connection is established and
data can be sent back and forth test yourself with these questions to see if this all makes
sense have a think about them discuss them with colleagues or study group and see how you go if
you want to see my answers they're available on the website for patreon supporters after a while
it will eventually be time to close the connection this could be because a process is complete say
a file transfer has finished or perhaps one device has taken too long and the other is
sick of waiting or maybe something is wrong with the connection and it needs to be terminated there
are two ways this can be done and either client or server can start the process firstly we'll
look at the graceful way to close a connection one of the devices will send a TCP segment with
both the fin and act flags sent fin is short for finished the other device replies with an
ACK message it will immediately follow up with a fin ACK message of its own which is acknowledged
with another act message so there's a lot going on just to close a connection for messages and
they appear to be duplicates so why are there two pairs of the same sets of messages the first pair
announces and acknowledges the intention to close the connection the respondent now needs to notify
the application that the connection is closing it might take a few seconds for the application
to deal with this the second pair of messages are sent only when the application is ready using two
pairs of messages will give the application time to respond before the connection is fully closed
now the second way to close a connection this is the non graceful method one device will need to
close a connection and send a TCP segment with the rst flag this means connection reset and that's it
there's no graceful waiting for the application there's no acknowledgments the connection is just
dropped this type of closure would only happen if there's an error it's common to see for example
if a client tries to connect to a port that's not open that is there's no application using
the port so a reset message is sent before the three-way handshake even completes but that
raises another interesting question why are there two ways to close a connection isn't one enough
well reset messages only happen if there's an error so this happens with troubleshooting so
if we're trying to narrow down a network issue one thing we could look for is reset messages
it's that time again see if you understand what's going on by pondering these questions
pause the video and give them some good thought we're now one step closer to understanding TCP
there's still one TCP video left where we look at error recovery and a flow control process
called windowing if this video was useful please like it so others can find it too see you soon