Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
08 coms 525 tcpip - tcp 1
1. 1
Transmission Control Protocol
K. PALANIVEL
Systems Analyst, Computer Centre
Pondicherry University, Puducherry – 605014.
LECTURE 8
COMS 525: TCPIP
COURSE
TOPIC
Part - I
3. Purpose of the Transport Layer
• The Transport layer provides for the segmentation
of data and the control necessary to reassemble
these pieces into the various communication
streams.
• Its primary responsibilities to accomplish this are:
– Tracking the individual communication between
applications on the source and destination hosts
– Segmenting data and managing each piece
– Reassembling the segments into streams of
application data
– Identifying the different applications
4. Identifying the Applications
• In order to pass data streams to the proper applications,
the Transport layer must identify the target application.
• To accomplish this, the Transport layer assigns an
application an identifier.
• The TCP/IP protocols call this identifier a port number.
• Each software process that needs to access the network is
assigned a port number unique in that host.
• This port number is used in the transport layer header to
indicate to which application that piece of data is
associated.
• The Transport layer is the link between the Application
layer and the lower layer that are responsible for network
transmission.
5. Data Requirements Vary
• Because different applications have different requirements,
there are multiple Transport layer protocols.
• For some applications, segments must arrive in a very
specific sequence in order to be processed successfully.
• In some cases, all of the data must be received for any of it
to be of use.
• In other cases, an application can tolerate some loss of data
during transmission over the network.
• In today's converged networks, applications with very
different transport needs may be communicating on the same
network.
• The different Transport layer protocols have different rules
allowing devices to handle these diverse data requirements.
7. Separating Multiple Communications
• Consider a computer connected to a network that is
simultaneously receiving and sending e-mail and instant
messages, viewing websites, file downloading and
conducting a VoIP phone call.
• Each of these applications is sending and receiving data
over the network at the same time.
• However,
– data from the phone call is not directed to the web
browser?
– text from an instant message does not appear in an e-
mail ?
9. Separating Multiple Communications
• At the Transport layer, each particular set of pieces
flowing between a source application and a destination
application is known as a conversation.
• To identify each segment of data, the Transport layer
adds to the piece a header containing binary data.
• This header contains fields of bits. It is the values in
these fields that enable different Transport layer
protocols to perform different functions.
11. Functions Specified Transport Layer
• Segmentation and Reassembly -
– Most networks have a limitation on the amount of
data that can be included in a single PDU.
– The Transport layer divides application data into
blocks of data that are an appropriate size.
– At the destination, the Transport layer reassembles
the data before sending it to the destination
application or service.
13. Functions Specified Transport Layer
• Conversation Multiplexing
– There may be many applications or services running
on each host in the network.
– Each of these applications or services is assigned an
address known as a port so that the Transport layer
can determine with which application or service the
data is identified.
14. Functions Specified Transport Layer
• In addition to using the information contained in the
headers, for the basic functions of data segmentation
and reassembly, some protocols at the Transport layer
provide:
– Connection-oriented conversations
– Reliable delivery
– Ordered data reconstruction
– Flow control
16. Functions Specified Transport Layer
• Establishing a Session
– The Transport layer can provide this connection orientation
by creating a sessions between the applications.
– These connections prepare the applications to communicate
with each other before any data is transmitted.
– Within these sessions, the data for a communication between
the two applications can be closely managed.
• Reliable Delivery
– For many reasons, it is possible for a piece of data to become
corrupted, or lost completely, as it is transmitted over the
network.
– The Transport layer can ensure that all pieces reach their
destination by having the source device to retransmit any data
that is lost.
17. Functions Specified Transport Layer
• Same Order Delivery
– Because networks may provide multiple routes that can have
different transmission times, data can arrive in the wrong order.
– By numbering and sequencing the segments, the Transport layer can
ensure that these segments are reassembled into the proper order.
• Flow Control
– Network hosts have limited resources, such as memory or
bandwidth.
– When Transport layer is aware that these resources are overtaxed,
some protocols can request that the sending application reduce the
rate of data flow.
– This is done at the Transport layer by regulating the amount of data
the source transmits as a group.
– Flow control can prevent the loss of segments on the network and
avoid the need for retransmission.
19. Overview
TCP = Transmission Control Protocol
Connection-oriented protocol
Provides a reliable unicast end-to-end byte stream
over an unreliable internetwork.
TCP
IP Internetwork
ByteStream
ByteStream
TCP
20. Connection-Oriented
Before any data transfer, TCP establishes a connection:
One TCP entity is waiting for a connection (“server”)
The other TCP entity (“client”) contacts the server
The actual procedure for setting up connections is more complex.
Each connection is full duplex
CLIENT SERVER
waiting for
connection
request
Request a connection
Accept a connection
Disconnect
Data Transer
25. Reliable
• Byte stream is broken up into chunks which are called
segments
• Receiver sends acknowledgements (ACKs) for segments
• TCP maintains a timer. If an ACK is not received in
time, the segment is retransmitted
• Detecting errors
• TCP has checksums for header and data. Segments with
invalid checksums are discarded
• Each byte that is transmitted has a sequence number
26. Three-Way Handshake : Step 1
• A TCP client begins the three-way handshake by sending a
segment with the SYN (Synchronize Sequence Number)
control flag set, indicating an initial value in the sequence
number field in the header.
• This initial value for the sequence number, known as the
Initial Sequence Number (ISN), is randomly chosen and is
used to begin tracking the flow of data from the client to
the server for this session.
• The ISN in the header of each segment is increased by one
for each byte of data sent from the client to the server as
the data conversation continues.
28. Three-Way Handshake : Step 2
• The TCP server needs to acknowledge the receipt of the SYN
segment from the client to establish the session from the client
to the server.
• The TCP server sends a segment back to the client with the
ACK flag set indicating that the Acknowledgment number is
significant.
• With this flag set in the segment, the client recognizes this as an
acknowledgement that the server received the SYN from the
TCP client.
• The value of the acknowledgment number field is equal to the
client initial sequence number plus 1.
• This establishes a session from the client to the server.
30. Three-Way Handshake : Step 3
• Finally, the TCP client responds with a segment
containing an ACK that is the response to the TCP
SYN sent by the server.
• There is no user data in this segment.
• The value in the acknowledgment number field
contains one more than the initial sequence number
received from the server.
• Once both sessions are established between client and
server, all additional segments exchanged in this
communication will have the ACK flag set.
32. TCP Termination
• To close a connection, the FIN (Finish) control flag in
the segment header must be set.
• To end each one-way TCP session, a two-way
handshake is used, consisting of a FIN segment and an
ACK segment.
• Therefore, to terminate a single conversation
supported by TCP, four exchanges are needed to end
both sessions.
33. TCP Termination
1. When the client has no more data to send in the
stream, it sends a segment with the FIN flag set.
2. The server sends an ACK to acknowledge the receipt
of the FIN to terminate the session from client to
server.
3. The server sends a FIN to the client, to terminate the
server to client session.
4. The client responds with an ACK to acknowledge the
FIN from the server.
35. Byte Stream Service
• To the lower layers, TCP handles data in blocks, the
segments.
• To the higher layers TCP handles data as a sequence of
bytes and does not identify boundaries between bytes
• So: Higher layers do not know about the beginning and
end of segments !
TCP
Application
1. write 100 bytes
2. write 20 bytes
queue of
bytes to be
transmitted TCP
queue of
bytes that
have been
received
Application
1. read 40 bytes
2. read 40 bytes
3. read 40 bytes
Segments
36. TCP Format
IP header TCP header TCP data
Sequence number (32 bits)
DATA
20 bytes 20 bytes
0 15 16 31
Source Port Number Destination Port Number
Acknowledgement number (32 bits)
window size
header
length
0 Flags
Options (if any)
TCP checksum urgent pointer
20bytes
• TCP segments have a 20 byte header with >= 0 bytes of data.
37. TCP header fields
Port Number:
◦ A port number identifies the endpoint of a connection.
◦ A pair <IP address, port number> identifies one endpoint
of a connection.
◦ Two pairs <client IP address, server port number> and
<server IP address, server port number> identify a TCP
connection.
TCP
IP
Applications
23 10480Ports:
TCP
IP
Applications
7 1680 Ports:
38. TCP header fields
Sequence Number (SeqNo):
Sequence number is 32 bits long.
So the range of SeqNo is
0 <= SeqNo <= 232 -1 4.3 Gbyte
Each sequence number identifies a byte in the byte
stream
Initial Sequence Number (ISN) of a connection is set
during connection establishment for ISN ?
39. TCP header fields
Acknowledgement Number (AckNo):
Acknowledgements are piggybacked, i.e., a segment
from A -> B can contain an acknowledgement for a data
sent in the B -> A direction
Q: Why is piggA hosts uses the AckNo field to send
acknowledgements. (If a host sends an AckNo in a
segment it sets the “ACK flag”)
The AckNo contains the next SeqNo that a hosts wants to
receive .
Example: The acknowledgement for a segment with
sequence numbers 0-1500 is AckNo=1501y backing good ?
40. TCP header fields
Acknowledge Number (cont’d)
TCP uses the sliding window flow protocol to regulate
the flow of traffic from sender to receiver
TCP uses the following variation of sliding window:
no NACKs (Negative ACKnowledgement)
only cumulative ACKs
Example:
Assume: Sender sends two segments with “1..1500” and
“1501..3000”, but receiver only gets the second
segment.
In this case, the receiver cannot acknowledge the second
packet. It can only send AckNo=1
41. TCP header fields
• Header Length ( 4bits):
– Length of header in 32-bit words
– Note that TCP header has variable length (with
minimum 20 bytes)
42. TCP header fields
Flag bits:
◦ URG: Urgent pointer is valid
If the bit is set, the following bytes contain an urgent
message in the range:
SeqNo <= urgent message <= SeqNo+urgent pointer
◦ ACK: Acknowledgement Number is valid
◦ PSH: PUSH Flag
Notification from sender to the receiver that the receiver
should pass all data that it has to the application.
Normally set by sender when the sender’s buffer is empty
43. TCP header fields
Flag bits:
◦ RST: Reset the connection
The flag causes the receiver to reset the connection
Receiver of a RST terminates the connection and
indicates higher layer application about the reset
◦ SYN: Synchronize sequence numbers
Sent in the first packet when initiating a connection
◦ FIN: Sender is finished with sending
Used for closing a connection
Both sides of a connection must send a FIN
44. TCP Header Fields
Window Size:
Each side of the connection advertises the window
size
Window size is the maximum number of bytes that a
receiver can accept.
Maximum window size is 216-1= 65535 bytes
TCP Checksum:
TCP checksum covers over both TCP header and
TCP data (also covers some parts of the IP header)
Urgent Pointer:
Only valid if URG flag is set
46. TCP Header Fields
Options:
NOP is used to pad TCP header to multiples of 4 bytes
Maximum Segment Size
Window Scale Options
Increases the TCP window from 16 to 32 bits, I.e.,
the window size is interpreted differently: What is
the different interpretation ?
This option can only be used in the SYN segment
(first segment) during connection establishment time
Timestamp Option
Can be used for roundtrip measurements
47. Connection Management in TCP
• Opening a TCP Connection
• Closing a TCP Connection
• Special Scenarios
• State Diagram
48. TCP Connection Establishment
TCP uses a three-way handshake to open a connection:
(1) ACTIVE OPEN: Client sends a segment with
SYN bit set *
port number of client
initial sequence number (ISN) of client
(2) PASSIVE OPEN: Server responds with a segment with
SYN bit set *
initial sequence number of server
ACK for ISN of client
(3) Client acknowledges by sending a segment with:
ACK ISN of server (* counts as one byte)
50. TCP Connection Termination
Each end of the data flow must be shut down
independently (“half-close”)
If one end is done it sends a FIN segment. This means
that no more data will be sent
Four steps involved:
(1) X sends a FIN to Y (active close)
(2) Y ACKs the FIN,
(at this time: Y can still send data to X)
(3) and Y sends a FIN to X (passive close)
(4) X ACKs the FIN.
51. TCP States
State Description
CLOSED No connection is active or pending
LISTEN The server is waiting for an incoming call
SYN RCVD A connection request has arrived; wait for Ack
SYN SENT The client has started to open a connection
ESTABLISHED Normal data transfer state
FIN WAIT 1 Client has said it is finished
FIN WAIT 2 Server has agreed to release
TIMED WAIT Wait for pending packets (“2MSL wait state”)
CLOSING Both Sides have tried to close simultanesously
CLOSE WAIT Server has initiated a release
LAST ACK Wait for pending packets
52. TCP States in “Normal” Connection Lifetime
SYN (SeqNo = x)
SYN (SeqNo = y, AckNo = x + 1 )
(AckNo = y + 1 )
SYN_SENT
(active open)
SYN_RCVD
ESTABLISHED
ESTABLISHED
FIN_WAIT_1
(active close)
LISTEN
(passive open)
FIN (SeqNo = m)
CLOSE_WAIT
(passive close)
(AckNo = m+ 1 )
FIN (SeqNo = n )
(AckNo = n+1)
LAST_ACK
FIN_WAIT_2
TIME_WAIT
CLOSED
53. TCP State Transition Diagram: Opening A Connection
CLOSED
LISTEN
SYN RCVD SYN SENT
ESTABLISHED
active open
send: SYN
recv: SYN, ACK
send: ACK
recv: SYN
send: SYN, ACK
recvd: ACK
send: . / .
recv:
RST
Application sends data
send: SYN
simultaneous open
recv: SYN
send: SYN, ACK
close or
timeout
passive open
send: . / .
recvd: FIN send: FIN
send:
FIN
54. TCP State Transition Diagram: Closing A Connection
FIN_WAIT_1
FIN_WAIT_2
ESTABLISHED
recv: FIN
send: ACK
recv: ACK
send: . / .
recvd: ACK
send: . / .
recv:
FIN, ACK
send: ACK
active close
send: FIN
TIME_WAIT
CLOSING
recv: FIN
send: ACK
CLOSED
Timeout
(2 MSL)
CLOSE_WAIT
LAST_ACK
passive close
recv: FIN
send: ACK
application
closes
send: FIN
recv: ACK
send: . / .
55. 2MSL(Maximum Segment Lifetime) Wait State
2MSL Wait State = TIME_WAIT
When TCP does an active close, and sends the final ACK,
the connection must stay in in the TIME_WAIT state for
twice the maximum segment lifetime.
2MSL= 2 * Maximum Segment Lifetime
Why?
TCP is given a chance to resent the final ACK. (Server
will timeout after sending the FIN segment and resend the
FIN)
The MSL is set to 2 minutes or 1 minute or 30 seconds.
56. Resetting Connections
• Resetting connections is done by setting the RST flag
• When is the RST flag set?
– Connection request arrives and no server process is
waiting on the destination port
– Abort (Terminate) a connection
Causes the receiver to throw away buffered data.
Receiver does not acknowledge the RST segment
57. Flow Control
• TCP also provides mechanisms for flow control.
• Flow control assists the reliability of TCP transmission
by adjusting the effective rate of data flow between the
two services in the session.
• When the source is informed that the specified amount
of data in the segments is received, it can continue
sending more data for this session.
• This Window Size field in the TCP header specifies the
amount of data that can be transmitted before an
acknowledgement must be received.
58. Flow Control
• The initial window size is determined during the
session startup via the three-way handshake.
• TCP feedback mechanism adjusts the effective rate of
data transmission to the maximum flow that the
network and destination device can support without
loss.
• TCP attempts to manage the rate of transmission so that
all data will be received and retransmissions will be
minimized.
60. Reducing Window Size
• Another way to control the data flow is to use dynamic
window sizes. When network resources are constrained,
TCP can reduce the window size to require that received
segments be acknowledged more frequently. This
effectively slows down the rate of transmission because
the source waits for data to be acknowledged more
frequently. The TCP receiving host sends the window
size value to the sending TCP to indicate the number of
bytes that it is prepared to receive as a part of this
session. If the destination needs to slow down the rate of
communication because of limited buffer memory, it can
send a smaller window size value to the source as part of
an acknowledgement.
61. Reducing Window Size
• Another way to control the data flow
• When network resources are constrained, TCP can reduce
the window size to require that received segments be
acknowledged more frequently.
• This effectively slows down the rate of transmission
because the source waits for data to be acknowledged more
frequently.
• The TCP receiving host sends the window size value to the
sending TCP to indicate the number of bytes that it is
prepared to receive as a part of this session.
• If the destination needs to slow down the rate of
communication because of limited buffer memory, it can
send a smaller window size value to the source as part of an
acknowledgement.
62. Reducing Window Size
• If a receiving host has congestion, it may respond to
the sending host with a segment with a reduced
window size.
• In this graphic, there was a loss of one of the segments.
• The receiver changed the window field in the TCP
header of the returning segments in this conversation
from 3000 down to 1500.
• This caused the sender to reduce the window size to
1500.
63. Reducing Window Size
• After periods of transmission with no data losses or
constrained resources, the receiver will begin to
increase the window field.
• This reduces the overhead on the network because
fewer acknowledgments need to be sent.
• Window size will continue to increase until there is
data loss, which will cause the window size to be
decreased.
• This dynamic increasing and decreasing of window
size is a continuous process in TCP, which determines
the optimum window size for each TCP session.
64. Reducing Window Size
• In highly efficient networks, window sizes may
become very large because data is not being lost. In
networks where the underlying infrastructure is being
stressed, the window size will likely remain small.