Applied Network Research Group Department of Computer Engineering, Kasetsart University
1/34
TCP: Transmission Control ProtocolPart I : Protocol basics
Surasak Sanguanpong
http://www.cpe.ku.ac.th/~nguanLast updated: July 30, 1999
Applied Network Research Group Department of Computer Engineering, Kasetsart University
2/34
Agenda
� Services provided by TCP� TCP format� How TCP reliability is achieved� Sliding window� TCP Connection� TCP State
Applied Network Research Group Department of Computer Engineering, Kasetsart University
3/34
TCP encapsulation
� with Ethernet frame
Ethernet hdr IP header TCP header data
segment
Applied Network Research Group Department of Computer Engineering, Kasetsart University
4/34
TCP & UDP Services
� TCP : Transmission Control Protocol� RFC 793� connection-oriented service� full duplex� reliable service by adding more overhead to manage
acknowledgment, flow control, timer
Applied Network Research Group Department of Computer Engineering, Kasetsart University
5/34
TCP: Transmission Control Protocol
� TCP performs typical transport layer functions:� passed data to relevant application-level services� mux and demux data from applications to and from IP
layer� error recovery� flow control data stream (avoid buffer overflow)
Applied Network Research Group Department of Computer Engineering, Kasetsart University
6/34
TCP properties
� byte stream with full duplex transferring� adaptive to LAN/WAN� congestion avoidance and control
Applied Network Research Group Department of Computer Engineering, Kasetsart University
7/34
TCP data stream
� TCP provides a full duplex service that simultaneousmanages two streams of data
� stream of octets passed between sender/receiver
applicationsend
receive
applicationreceivesend
Applied Network Research Group Department of Computer Engineering, Kasetsart University
8/34
Ports� Port - a 16 bit address allocated for the most common
application layer services� UDP and TCP use port addressing to deliver info to
applications� Servers are known by ports number
z FTP 20, TELNET 23, SMTP 25, HTTP 80
� Port numbers are generally allocated by� 0 --not used� 1-255 --Reserved ports for well-known services� 256-1023 --Other reserved ports� 1024-65535 --user-defined server ports
� Unix store general used ports in /etc/services
Applications
Transport
Network access
1 2 3 4( ) ( ) ( ) ( )
Applied Network Research Group Department of Computer Engineering, Kasetsart University
9/34
Sockets
� socket : a pair of the IP address and the portnumber
IP address is unique to a node, the port is unique on a node
the socket gives a unique identification ofan application layer services
IP address is unique to a node, the port is unique on a node
the socket gives a unique identification ofan application layer services
<158.108.33.3, 3000>
IP address port number
Applied Network Research Group Department of Computer Engineering, Kasetsart University
10/34
Socket address
� A connection is identified by the socket address at its toends� client socket: 158.108.33.3,3000; 158.108.2.71,21� server socket: 158.108.2.71,21; 158.108.33.3,3000;
port : 3000 port : 21connection
client server
IP : 158.108.33.3 IP : 158.108.2.71
Applied Network Research Group Department of Computer Engineering, Kasetsart University
11/34
Socket multiple connection
� server’s unique socket address can be accessedsimultaneously by clients
port : 3000
port : 21connection
clientserverIP : 158.108.33.2
IP : 158.108.2.71
port : 3120
clientIP : 158.108.33.3
Applied Network Research Group Department of Computer Engineering, Kasetsart University
12/34
Transmission Control Protocol
� TCP passed block of data to IP, consisting of the TCP headerand application layer data, called segment
� adding reliability in TCP is achieved by� Error detection and correction (due to segments corrupted)� Flow control (prevent a transmitter overrunning a receiver owing a
resource limitations)� Resequencing (IP can deliver datagrams in any order)� Removing duplicate segments (due to error-recovery mechanisms
used by TCP)
Applied Network Research Group Department of Computer Engineering, Kasetsart University
13/34
How TCP handles reliability
� Using sequence numbers to identify data� positive acknowledgments of data received in the correct
sequence� retransmission of segments which have not been
acknowledged within a (variable) time limit� Let’s see these mechanisms in TCP header
Applied Network Research Group Department of Computer Engineering, Kasetsart University
14/34
TCP header
source port:16 destination port:16 sequence number:32 acknowledgment number:32data offs:4 resv:6 flag:6 window size:16 checksum :16 urgent pointer:16 options and padding
0 15 16 31
Applied Network Research Group Department of Computer Engineering, Kasetsart University
15/34
TCP header details (I)
� source, destination port:16,16 - identify applications at endsof the connection
� sequence:32 - indicates 1st data octet in this segment� acknowledgment:32 - next expected sequence number,
valid only when the ACK bit (reside in flag) is set
Applied Network Research Group Department of Computer Engineering, Kasetsart University
16/34
TCP header details (II)
� data offset:4 - 32 bit words offset tells thereceiver where user data begins
� reserved:6 -not used� flag:6
z URG : validity of urgent pointer fieldz ACK : validity of acknowledge fieldz PSH : push request (pass segment to appl layer immediately)z RST : reset the connectionz SYN : initial synchronizationz FIN : sender at end of byte stream
Applied Network Research Group Department of Computer Engineering, Kasetsart University
17/34
TCP header details (III)
� window:16 - advertise amount of buffer space this node hasallocated
� checksum:16 - 16 bits 1’s complement of pseudo header,TCP header and data
� urgent pointer:16 - byte position of data that should beprocessed first
� options - variable length option e.g. MSS (max segmentsize) tells destination node
Applied Network Research Group Department of Computer Engineering, Kasetsart University
18/34
Sliding window principle
� send and wait for acknowledgment� no ACK within a certain time, retransmit the packet� use for flow control :
� prevent sender from overloading receiver with data, e.g.high-performance server to slow PC
� congestion inside network, e.g. router performance, slow linkspeed
� How to provide flow control?� set the appropriate size of sliding window size
Applied Network Research Group Department of Computer Engineering, Kasetsart University
19/34
Sliding window flow control
� Receiver “ advertises” it’s windows size inacknowledgments
� Sender will adjusts its allowed to send pointer as receiver’sadvertisement
no! no! I can load only 200 kg.
Applied Network Research Group Department of Computer Engineering, Kasetsart University
20/34
Sliding window: small window size
SENDER RECEIVER
Send 1 Receive 1Ack 2
Send 2 Receive 2Ack 3
� 1 byte window size utilizes efficiency of channel in half(half-duplex transmission)
� why not send many packets and get back cumulative ACK?
window size =1
Applied Network Research Group Department of Computer Engineering, Kasetsart University
21/34
Sliding window: larger window size
SENDERRECEIVER
Send 1Send 2Send 3
Receive 1Receive 2Receive 3
Receive ACK 4
� A larger window size allows more data to be transmitted pendingacknowledgment
� Window size specifies how many bytes the receiver is willing toaccept
window size =3
Send ACK 4Send 4Send 5Send 6
Receive 4Receive 5Receive 6
Receive ACK 4 Send ACK 4
Applied Network Research Group Department of Computer Engineering, Kasetsart University
22/34
Sequence number in segment
� Data continuously sent more than segment� need not to wait for acknowledgment every segment
data bytes 500-599
data bytes 600-699
data bytes 700-799
host A host B
ACK 800
I received all up to 799!
Applied Network Research Group Department of Computer Engineering, Kasetsart University
23/34
Retransmit a loss segment
data bytes 500-599
data bytes 600-699
data bytes 700-799
host A host B
ACK 700
data bytes 700-799
Applied Network Research Group Department of Computer Engineering, Kasetsart University
24/34
Error recovery (I)
� receiver has to send ACK with sequence number� sender reset timer when receives ACK
segment #i,start timer
host A host B
cancel timer
ACK
Applied Network Research Group Department of Computer Engineering, Kasetsart University
25/34
Error recovery (II)
� on time out, sender will retransmit the segment� this mechanism is used for error recovery
segment #i, start timer
host A host B
cancel timer
ACK
timer expires, resend #i
Applied Network Research Group Department of Computer Engineering, Kasetsart University
26/34
Sliding window buffer� sender groups its packet to be transmitted with window indication
1000… 1099 1100… 1199 ....999 1200…
sent and ACK
sent and not ACKed
can send ASAP can’t send now
SndWnd
SndUna SndNxt SndUna+SndWnd
offered windows
Applied Network Research Group Department of Computer Engineering, Kasetsart University
27/34
1300…
....999
Sliding window example� movement of the right and left edges of the window
1000… 1099 1100… 1199 1200…
SndUna, SndNxt SndUna+SndWnd
1000… 1099 1100… 1199....999 1200…
SndUna SndUna+SndWndSndNxt
....999
1000… 1099 1100... 1199 1200…
SndUna SndUna+SndWndSndNxt,
....999 1000... 1099 1100… 1199 1200 1299
SndUna SndUna+SndWndSndNxt
initial
send 100 bytes
more 100 bytes
ACK 100 bytes
Applied Network Research Group Department of Computer Engineering, Kasetsart University
28/34
TCP in actions
� before data could be transferred, a connection must beopened� servers do passive open (listen)� clients do active open (connect)
� when it finished, the connection is closed� TCP has general 3 phases
� connection setup phase� data phase� connection close phase
Applied Network Research Group Department of Computer Engineering, Kasetsart University
29/34
TCP connection establishment
(1) send SYN (seq=x)
host A host B
(3) send ACK (ack=y+1)
(2) send SYN (seq =y, ack=x+1)
� TCP uses 3-way handshake to establish a connection� exchange the sequence number� ensures that both ends are ready and sync sequence number
connection is setup!
Applied Network Research Group Department of Computer Engineering, Kasetsart University
30/34
Transfer phase
� simple example with terminal connection such as Telnet.Host echoes back each received character
SEQ=92, ACK=109DATA=“w”
host A host B
SEQ=93, ACK=110DATA=“…”
SEQ=109, ACK=93 DATA=“w”
Host echoes back “w”
Applied Network Research Group Department of Computer Engineering, Kasetsart University
31/34
TCP Connection close
� use FIN flag to close connection
SEQ=x, FIN
host A host B
ACK=y+1
ACK=x+1 SEQ=Y
inform application
application response withSEQ=y, FIN
Applied Network Research Group Department of Computer Engineering, Kasetsart University
32/34
Open/Close mechanisms
� Half open - one end has closed, aborted without the knowledgeof the other end
� host may be crashed, power off� no detection if no data transfer� reset segment (RST bit) is sent when detected
� Half close - one end of connection terminated its output, but stillreceiving data from the other end
� Simultaneous open- both end perform an active open to eachother
� Simultaneous close - both end perform an active open to eachother
Applied Network Research Group Department of Computer Engineering, Kasetsart University
33/34
TCP state diagram : open CLOSED
start
appl passive opens;send nothing
LISTEN
passive open
appl active opens;
send SYN SYN_SENT
active open
SYN_RCVD
got SYN; s
end SYN,A
CK
ESTABLISHED
got SYN, A
CK;
send A
CK
got ACK;send nothing
got SYN;
send SYN, ACK
simultaneous open
got RST
appl closes;send FIN
appl closes;
or time-out
(normal) client transition(normal) server transition
Applied Network Research Group Department of Computer Engineering, Kasetsart University
34/34
TCP state diagram : close
ESTABLISHED
get FIN, ACK;
send: ACK
appl close;
send FINappl closes;
send FIN
FIN_WAIT_1 CLOSING
FIN_WAIT_2 TIME_WAIT
got ACK;send nothing
got FIN;
send ACK
got ACK;send nothing
got FIN;send ACK
2MSL time-out
CLOSE_WAIT
LAST_ACK
appl closes;send FIN
got FIN;send ACK
got ACK;send nothing
back to CLOSED
active close
passive close
simultaneous close
(normal) client transition(normal) server transition