TCP � TCP provides the end-to-end reliable CSCE 515: connection that IP alone cannot support Computer Network Programming � The TCP protocol ------ TCP Details � Segment format � Connection Creation Wenyuan Xu � Flow control Department of Computer Science and � Congestion control Engineering University of South Carolina � Connection termination 9/15/2008 CSCE515 – Computer Network Programming More... TCP Segment Format � Every TCP segment includes a Sequence Number that refers to the first byte of data 0 15 16 31 included in the segment. source port number destination port number sequence number 20 bytes � Every TCP segment includes a Request acknowledgment number U A P R S F Number ( Acknowledgement Number ) that header window size reserved R C S S Y I length G K H T N N indicates the byte number of the next data TCP checksum urgent pointer that is expected to be received. option (if any) � All bytes up through this number have already been received. data (if any) 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming And more... And more… � MSS: Maximum segment size (A TCP � There are a bunch of control flags: option) � URG: urgent data included. � ACK: this segment is (among other things) an acknowledgement. � Window: Every ACK includes a Window � RST: error - abort the session. field that tells the sender how many bytes � SYN: Used to establish connection; synchronize Sequence Numbers (setup) it can send before the receiver will have to � FIN: polite connection termination. toss it away (due to fixed buffer size). 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming
TCP Connection Establishment TCP Lingo – Three-way handshake � When a client requests a connection, it sends a “SYN” segment (a special TCP Client Server segment) to the server port. SYN 1 SYN “I want to talk, and ISN= X I’m starting with ISN= X � SYN stands for synchronize . The SYN byte number X+1 ”. message includes the client’s ISN. SYN 2 “OK, I’m here and I’ll time SYN talk. My first byte will ISN= Y ACK= X +1 ISN= Y ACK= X +1 be called number Y+1, � ISN is Initial Sequence Number. and I know your first byte will be number X+1” “Got it - you start ACK= Y +1 3 at byte number Y+1”. ACK= Y +1 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming TCP Connection Creation Client Starts � A server accepts a connection. � A client starts by sending a SYN segment with the following information: � Must be looking for new connections! � Client’s ISN (generated pseudo-randomly) � Maximum Receive Window for client. � A client requests a connection. � Optionally (but usually) MSS (largest � Must know where the server is! datagram accepted). � No payload! (Only TCP headers) 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming Sever Response Finally � When a waiting server sees a new � When the Server’s SYN is received, the connection request, the server sends client sends back an ACK with: back a SYN segment with: � Request Number is Server’s ISN+1 � Server’s ISN (generated pseudo-randomly) � Request Number is Client ISN+1 � Maximum Receive Window for server. � Optionally (but usually) MSS � No payload! (Only TCP headers) 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming
Why 3-Way? TCP Data and ACK � Once the connection is established, data � Why is the third message necessary? can be sent. � Each data segment includes a sequence � HINTS: number identifying the first byte in the � TCP is a reliable service. segment. � IP delivers each TCP segment. � Each ACK segment includes a request � IP is not reliable. number indicating what data has been received. (bytes instead of packets) 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming Buffering Important Information in TCP/IP packet headers � The TCP layer doesn’t know when the application will ask for any received data. Sequence N SEQ number of Number of Send first data byte � Both the client and server allocate buffers to bytes in in packet packet (N) hold incoming and outgoing data (TCP does this) (SEQ) � incoming data -> it’s ready when application asks for it. Sequence � outgoing data-> it can be retransmitted should error Window size number of ACK WIN ACK bit set at the receiver occurs next expected (WIN) Recv byte (ACK) � Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment). Contained in IP header Contained in TCP header 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming Send Buffers Recv Buffers � The application gives the TCP layer some data � The received data is put in the recv buffer, to send. where it stays until the application reads data � The data is put in a send buffer, where it stays until the data is ACK’d. � The Recv Buffer won’t accept data from network � it has to stay, as it might need to be sent again! unless (or until) there is buffer space, and the � The TCP layer won’t accept data from the sequence number is within its sliding windows. application unless (or until) there is buffer space. � The recv buffer can store out of order data as long as there is buffer space and the sequence number is within its sliding windows 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming
ACKs TCP Flow Control � A receiver doesn’t have to ACK every recv’s buffer Sender receiver 0 4K segment (it can ACK many segments with Application does empty a 2K write a single ACK segment). 2K SEQ=0 2K � Each ACK can also contain outgoing data ACK = 2048 WIN = 2048 Application does (piggybacking). a 3K write 2K SEQ=2048 � If a sender doesn’t get an ACK after some Full Sender is ACK = 4096 WIN = 0 time limit (MSL) it resends the data. blocked Application reads 2k ACK = 4096 WIN = 2048 2K Sender may send up to 2k 1k SEQ=4096 1K 2K 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming TCP Segment Order Termination � Most TCP implementations will accept out-of- � The TCP layer can send a RST segment order segments (if there is room in the buffer). that terminates a connection if something � Once the missing segments arrive, a single ACK is wrong. can be sent for the whole thing. � Usually the application tells TCP to � Remember: IP delivers TCP segments, and IP in terminate the connection politely with a not reliable - IP datagrams can be lost or arrive FIN segment. out of order. 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming TCP Termination FIN App1 App2 � Either end of the connection can initiate termination. “I have no more FIN 1 FIN data for you” SN= X SN= X � A FIN is sent, which means the application is done sending data. “ OK, I understand you ACK= X +1 2 are done sending .” ACK= X +1 Release connection � The FIN is ACK’d. Release connection ... � The other end must now send a FIN. “OK - Now I’m also 3 FIN FIN done sending data”. � That FIN must be ACK’d. SN= Y SN= Y “Over and Out, Goodbye” ACK= Y +1 4 Release connection ACK= Y +1 Release connection 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming
TCP Termination TCP TIME_WAIT App1 App2 � Once a TCP connection has been “I have no more terminated (the last ACK sent) there is FIN 1 FIN data for you” SN= X FIN_WAIT_1 SN= X some unfinished business: “ OK, I understand you � What if the ACK is lost? The last FIN will ACK= X +1 2 are done sending .” ACK= X +1 be resent and it must be ACK’d. CLOSE_WAIT FIN_WAIT_2 ... � What if there are lost or duplicated “OK - Now I’m also 3 segments that finally reach the destination FIN FIN done sending data”. SN= Y LAST_ACK SN= Y after a long delay? “Over and Out, � TCP hangs out for a while to handle Goodbye” ACK= Y +1 4 TIME_WAIT ACK= Y +1 these situations. CLOSED 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming Test Questions Assignment & Next time � Reading: � Why is a 3-way handshake necessary? � UNP 2.4, 2.6, 2.7 ** � Who sends the first FIN - the server or the client? � Next Lecture: � Once the connection is established, what is the difference between the operation of � TCP Socket Programming the server’s TCP layer and the client’s TCP layer? � What happens if a bad guy can guess ISNs? 9/15/2008 CSCE515 – Computer Network Programming 9/15/2008 CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Recommend
More recommend