Department of Computer Science and Engineering University of South Carolina
Segment
format Connection Creation Flow control Congestion control Connection termination 9/15/2008
TCP Connection Establishment
TCP Segment Format 0
15 16
source port number
– Three-way handshake 31
Client
destination port number
sequence number acknowledgment number reserved
U A P R S F R C S S Y I G K H T N N
TCP checksum
20 bytes
window size urgent pointer
option (if any)
Server SYN SYN ISN=X ISN=X
“I want to talk, and I’m starting with byte number X+1”.
time
header length
CSCE515 – Computer Network Programming
1 “OK, I’m here and I’ll talk. My first byte will be called number Y+1, and I know your first byte will be number X+1”
SYN SYN 2 ISN=Y ISN=YACK=X+1 ACK=X+1
“Got it - you start at byte number Y+1”.
ACK=Y+1 ACK=Y+1
3
data (if any)
9/15/2008
CSCE515 – Computer Network Programming
TCP Data and ACK
9/15/2008
CSCE515 – Computer Network Programming
Important Information in TCP/IP packet headers
Once the connection is established, data can be sent. Each data segment includes a sequence number identifying the first byte in the segment. Each ACK segment includes a request number indicating what data has been received. (bytes instead of packets)
N SEQ
Number of bytes in packet (N)
ACK WIN
ACK bit set
Send
Recv
Contained in IP header 9/15/2008
CSCE515 – Computer Network Programming
9/15/2008
Sequence number of first data byte in packet (SEQ)
Sequence number of next expected byte (ACK)
Window size at the receiver (WIN)
Contained in TCP header
CSCE515 – Computer Network Programming
TCP Flow Control
FIN receiver recv’s buffer 4K 0 empty
Sender Application does a 2K write
2K
SEQ=0
2K ACK = 2048 WIN
= 2048
Application does a 3K write 2K
SEQ=2048
Full
Sender is blocked
ACK = 4096 WIN ACK = 4096 WIN
Sender may send up to 2k
1k
=0
Application reads 2k
= 2048
2K
SEQ=4096
1K
9/15/2008
2K
CSCE515 – Computer Network Programming
9/15/2008
TCP Termination App2 FIN FIN SN=X SN=X
ACK=X+1 ACK=X+1
1 “OK, I understand you are done sending.”
2
CLOSE_WAIT
...
FIN_WAIT_2
FIN FIN SN=Y SN=Y “Over and Out, Goodbye” TIME_WAIT
ACK=Y+1 ACK=Y+1
9/15/2008
4 CLOSED
9/15/2008
Client-Server Communication (TCP) socket()
int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
TCP Client int listen(int sockfd, int backlog); int socket(int family, int type, int protocol);
bind()
connection establishment
write()
data(request)
read() process request
read() close()
write()
end-of-file no tification int close(int sockfd); int close(int sockfd);
9/15/2008
CSCE515 – Computer Network Programming
Creating a TCP socket int socket(int family,int type,int proto);
listen()
blocks until connection from client
data(reply)
CSCE515 – Computer Network Programming
TCP Server well-known port
accept() int accept(int sockfd, void *addr, int *addrlen); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
connect()
Creating a passive mode (server) socket. Establishing an application-level connection. send/receive data. Terminating a connection.
“OK - Now I’m also done sending data”. LAST_ACK
3
CSCE515 – Computer Network Programming
socket()
CSCE515 – Computer Network Programming
TCP Sockets Programming
App1 “I have no more data for you” FIN_WAIT_1
Either end of the connection can initiate termination. A FIN is sent, which means the application is done sending data. The FIN is ACK’d. The other end must now send a FIN. That FIN must be ACK’d.
int sock; sock = socket( AF_INET, SOCK_STREAM, 0); if (sock