CS 4396 Computer Networks Lab: Transmission Control Protocol (TCP)
CS 4396 Computer Networks Lab: Transmission Control Protocol (TCP)
Overview
TCP = Transmission Control Protocol Connection-oriented protocol Provides a reliable unicast end-to-end byte stream over an unreliable internetwork.
Byte Stream Byte Stream
TCP
TCP
IP Internetwork
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. SERVER Each connection is CLIENT Request a co nnection full duplex
Accept a c onnection
Data Transer
Disconnect
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
4
Application
1. read 40 bytes 2. read 40 bytes 3. read 40 bytes
TCP
Segments
TCP
TCP Format
TCP segments have a 20 byte header with >= 0 bytes of data.
IP header
20 bytes 0 Source Port Number
TCP header
20 bytes 15 16
TCP data
31
Flags
TCP checksum
Ports:
23 80 104 TCP IP
80 16 TCP IP
Ports:
Each sequence number identifies a byte in the byte stream Initial Sequence Number (ISN) of a connection is set during connection establishment Q: What are possible requirements for ISN ?
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=1501
9
11
12
13
14
17
18
Three-Way Handshake
aida.poly.edu
mng.poly.edu
SYN (Seq
No = x)
(SeqNo = x
+1, AckNo =
y+1)
19
20
Three-Way Handshake
aida.poly.edu
S 103188 0193:1031 880193(0) win 16384 <mss 146 0 , .. .> 586(0) 88586:172488 S 1724 <mss 1460> 194 win 8760 ack 1031880
mng.poly.edu
ack 172488
20
21
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.
22
1 mng.poly.edu.telnet > aida.poly.edu.1121: F 172488734:172488734(0) ack 1031880221 win 8733 2 aida.poly.edu.1121 > mng.poly.edu.telnet: . ack 172488735 win 17484 3 aida.poly.edu.1121 > mng.poly.edu.telnet: F 1031880221:1031880221(0) ack 172488735 win 17520 4 mng.poly.edu.telnet > aida.poly.edu.1121: . ack 1031880222 win 8733
23
aida.poly.edu
F 172488734:172488734(0) ack 1031880221 win 8733 . ack 1 7 2488735 win 174 84
mng.poly.edu
24
TCP States
State CLOSED LISTEN SYN RCVD SYN SENT ESTABLISHED FIN WAIT 1 FIN WAIT 2 TIMED WAIT CLOSING CLOSE WAIT LAST ACK Description No connection is active or pending The server is waiting for an incoming call A connection request has arrived; wait for Ack The client has started to open a connection Normal data transfer state Client has said it is finished Server has agreed to release Wait for pending packets (2MSL wait state) Both Sides have tried to close simultanesously Server has initiated a release Wait for pending packets
25
SYN (SeqNo = x)
LISTEN
(passive open)
y, AckN YN (SeqNo = S
o=x+1)
SYN_RCVD
(AckNo = y + 1 )
ESTABLISHED ESTABLISHED FIN_WAIT_1 (active close) FIN_WAIT_2
FIN (SeqNo = m)
(AckNo = m+ 1 )
CLOSE_WAIT (passive close)
FIN (SeqNo = n )
TIME_WAIT
(AckNo =
LAST_ACK
n+1)
CLOSED
26
LISTEN
recv: RST recv: SYN send: SYN, ACK
SYN RCVD
recvd: ACK send: . / . send: FIN recvd: FIN
SYN SENT
recv: SYN, ACK send: ACK
ESTABLISHED
send: FIN
27
ESTABLISHED
recv: FIN send: ACK
FIN_WAIT_1
recv: ACK send: . / .
CLOSING
recvd: ACK send: . / .
CLOSE_WAIT
application closes send: FIN
LAST_ACK
FIN_WAIT_2
TIME_WAIT
Timeout (2 MSL) recv: ACK send: . / .
CLOSED
28
29
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
30