TCP I - LINK@KoreaTech
Download
Report
Transcript TCP I - LINK@KoreaTech
TCP Tutorial
Internet Computing Laboratory @ KUT
(http://icl.kut.ac.kr)
Youn-Hee Han
It is licensed under a Creative Commons Attribution 2.5 License
TCP Introduction
2
Computer Network
TCP Features
Connection-oriented Service with Reliability
Information unit is a “segment”
응용 데이터는 TCP가 전송하기 적합한 크기로 나뉨
반면, UDP는 각 응용에 맞는 UDP 데이터그램을 직접 생성
Byte stream service
3
Two application exchanges 8-bit (byte) stream date
TCP does not interpreter the bytes’ contents
No Message Boundary
Computer Network
TCP Features
Sender TCP set a timer whenever sending a segment
Sender waits an ACK message after sending a segment
Receiver TCP should send an ACK message to the sender
4
Sender re-sends the segment if it does not receive an ACK message
before the timer expires
Computer Network
TCP Features
TCP segment is delivered as IP datagram
IP datagram may be out of order TCP segment may be out of order
Receiver TCP re-organizes the segments according to the correct
sequence
Receive TCP eliminates the duplicated segment
5
Computer Network
TCP Features
TCP provides flow control
TCP 연결의 각 종단은 유한한 버퍼를 가짐
수신측 TCP는 버퍼용량을 초과하지 않는 범위의 데이터를 받음
TCP provides congestion control
네트워크가 Congestion 하여 Ack가 늦게 오거나 Ack가 오지 않는 경우
보내는 데이터의 양을 줄임
TCP utilizes the checksum to detect an error on the header and
data
데이터가 전송 중에 변화되었는지 검출하는 것이 목적
기본적으로 오류가 난 세그먼트는 받지 않은 것으로 취급.
송신자의 타임아웃과 재전송을 기다림.
6
IP provides 16 bit checksum for IP header.
TCP provides 32 bit checksum for TCP header and TCP data.
Computer Network
TCP Features
Full-duplex communication
Bi-directional data flow
Data can be transmitted while receiving
Use of "Piggyback"
Data packets can convey feedback information in the opposite direction
7
Computer Network
TCP Features
TCP may delay data transfer.
If TCP has not received enough data from the application
TCP tries to aggregate data as much as possible.
8
Applications that use TCP do not know when data will be sent.
Computer Network
TCP Ports, Connections, And Endpoints
Endpoint of communication is application program
TCP uses protocol port number to identify application
TCP defines an “endpoint” to be a pair of integers (host, port)
Where host is the IP address for a host
TCP connection between two endpoints identified by four
items
9
Sender’s IP address
Sender’s protocol port number
Receiver’s IP address
Receiver’s protocol port number
Computer Network
Socket
Socket: a door between application process and endend-transport protocol (UCP or TCP)
controlled by
application
developer
controlled by
operating
system
socket
TCP with
buffers,
variables
host or
server
10
process
process
internet
socket
TCP with
buffers,
variables
controlled by
application
developer
controlled by
operating
system
host or
server
Computer Network
TCP Segment Header
20 bytes + options
One of options: Maximum Segment Size (MSS)
0
15 16
16-bit source port number
31
16-bit destination port number
32-bit sequence number
32-bit acknowledgment number
4-bit header
length
reserved
(6bits)
U A P R S F
R C S S Y I
G K H T N N
16-bit TCP checksum
Counting by bytes
of data (not segments!)
16-bit window sizes
16-bit urgent pointer
options (if any)
data (if any)
11
Computer Network
TCP Segment Header
Each connection identified with 4-tuple:
(SrcPort, SrcIPAddr, DsrPort, DstIPAddr)
Sliding window + flow control
acknowledgment(Num), SequenceNum, AdvertisedWindow
Data (SequenceNum)
Receiver
Sender
Acknow ledgment +
AdvertisedWindow
Flags
SYN, FIN, RESET, PUSH, URG, ACK
Checksum
12
pseudo header + TCP header + data
Computer Network
Six flags and Option
Six flag bits
URG - The urgent pointer is valid. (generally not used)
ACK - The acknowledgment number is valid
PSH - The receiver pass this data to the application as soon as possible.
(generally not used)
RST - Reset the connection.
SYN - Synchronize sequence numbers to initiate a connection.
FIN - The sender is finished sending data.
Urgent pointer
URG플래그가 설정되어 있을 때만 유효함.
송신 측이 상대 편에게 긴급한 데이터를 보내는 방법 (out-of-band data)
Option field
13
MSS라고 불리는 maximum segment size option을 위한 자리
Connection establish (SYN이 set됨)하기 위한 메시지에 이 option을
지정함
Computer Network
TCP connection establishment
3 way handshake
Server
Client
SYN
ISN=X
1
time
2
SYN
ISN=Y ACK=X+1
ACK=Y+1
1
2
Server: “OK, I’m here and I’ll
talk. My first byte will be
called number Y, and I
know your first byte will be
number X+1”.
3
3
14
Client: “I want to talk, and I’m
starting with byte number
X”.
Client: “Got it - you start at
byte number Y+1”.
Computer Network
TCP connection establishment
3 way handshake
Site 1
Site 2
Send SYN seq. #=x
Rcv SYN + ACK segt
Send ACK seq.#=y+1
Rcv SYN segment
Send SYN seq.#=y,
ACK #=x+1
Rcv ACK segment
Initial sequence numbers, x & y, are chosen randomly
Guarantees both sides ready & know it, and sets initial sequence
numbers, also sets window & mss
Once connection established, data can flow in both directions,
equally well, there is no master or slave
15
Computer Network
TCP connection establishment
Passive And Active Opens
Two sides of a connection
Client
Server
One side initiates contact
A client program (Site 1)
Uses TCP’s active open
One side waits for contact
16
A server program (Site 2)
Uses TCP’s passive open
Computer Network
TCP close connection
The TCP layer can send a RST segment that terminates a
connection if something is wrong.
Usually the application tells TCP to terminate the connection
politely with a FIN segment.
FIN segment
17
Either end of the connection can initiate termination.
Application 1 call “close()” function
A FIN is sent, which means the application is done sending data.
The FIN is ACK’d.
Application 2 call “close()” function
A FIN is also sent.
That FIN must be ACK’d.
Computer Network
TCP close connection
Modified 3 way handshake (or 4 way termination)
1
App1: “I have no more data for
you”.
2
App2: “OK, I understand you are
done sending.”
App2
App1
FIN
SN=X
2
……. pause…
...
ACK=X+1
1
FIN
SN=Y
ACK=Y+1
18
3
3
App2: “OK - Now I’m also done
sending data”.
4
App1: “I understand , Goodbye”
4
Computer Network
TCP close connection
Modified 3 way handshake (or 4 way termination)
(App closes)
Send FIN seq=s
Site 1
Rcv ACK segment
Rcv FIN segment
Send ACK s+1 (inform app)
Site 2
(app closes connection)
Send FIN seq=t, ACK s+1
Rcv FIN + ACK seg
Send ACK t+1
Timed wait
Receive ACK segment
App tells TCP to close, TCP sends remaining data & waits for ACK, then sends FIN &
EOF (End of Data)
Site 2 TCP ACKs FIN, tells its application “EOF”
Site 2 sends FIN when its app closes connection (may be long delay)
19
exit(0) automatically calls close()
Computer Network
State Transition Diagram
CLOSED
Active open /SYN
ESTABLISHED state to the CLOSED
1.
2.
3.
Passive open
This side closes first:
ESTABLISHED >> FIN_WAIT_1
>> FIN_WAIT_2 >> TIMEWAIT
>> CLOSED
The other side closes first:
ESTABLISHED >>
CLOSE_WAIT >> LAST_ACK >>
CLOSED
Both sides close at the same
time:
ESTABLISHED >> FIN_WAIT_1
>> CLOSINC >> TIME_WAIT
>> CLOSED.
Close
LISTEN
SYN_RCVD
SYN/SYN + ACK
Send /SYN
SYN/SYN + ACK
ACK
SYN_SENT
SYN + ACK/ACK
ESTABLISHED
Close/FIN
Close /FIN
FIN/ACK
FIN_WAIT_1
ACK
FIN_WAIT_2
CLOSE_WAIT
AC FIN/ACK
K
+
FI
N/
AC
K
FIN/ACK
20
Close
Close/FIN
CLOSING
LAST_ACK
ACK Timeout after tw o
segment lifetimes
TIME_WAIT
ACK
CLOSED
Computer Network