Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 515: Computer Network Programming ------ TCP Details Wenyuan Xu Department of Computer Science and Engineering.

Similar presentations


Presentation on theme: "CSCE 515: Computer Network Programming ------ TCP Details Wenyuan Xu Department of Computer Science and Engineering."— Presentation transcript:

1 CSCE 515: Computer Network Programming ------ TCP Details Wenyuan Xu http://www.cse.sc.edu/~wyxu/csce515f07.html Department of Computer Science and Engineering University of South Carolina

2 CSCE515 – Computer Network Programming 2007 TCP TCP provides the end-to-end reliable connection that IP alone cannot support The TCP protocol  Frame format  Connection Creation  Flow control  Congestion control  Connection termination

3 CSCE515 – Computer Network Programming 2007 TCP Segment Format 0151631 20 bytes destination port number urgent pointerTCP checksum option (if any) source port number window size sequence number acknowledgment number header length reserved URGURG ACKACK PSHPSH RSTRST SYNSYN FINFIN data (if any)

4 CSCE515 – Computer Network Programming 2007 ClientServer SYN ISN=X SYN ISN=X 1 SYN ISN=Y ACK=X+1 SYN ISN=Y ACK=X+1 2 ACK=Y+1 3 time TCP Connection Establishment – Three-way handshake “I want to talk, and I’m starting with byte number X+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” “Got it - you start at byte number Y+1”.

5 CSCE515 – Computer Network Programming 2007 TCP Data and ACK 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)

6 CSCE515 – Computer Network Programming 2007 Important Information in TCP/IP packet headers Send NSEQ Recv ACKWIN Number of bytes in packet (N) ACK bit set Sequence number of next expected byte (ACK) Sequence number of first data byte in packet (SEQ) Window size at the receiver (WIN) Contained in IP headerContained in TCP header

7 CSCE515 – Computer Network Programming 2007 Buffering The TCP layer doesn’t know when the application will ask for any received data. Both the client and server allocate buffers to hold incoming and outgoing data (TCP does this)  incoming data -> it’s ready when application asks for it.  outgoing data-> it can be retransmitted should error occurs Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment).

8 CSCE515 – Computer Network Programming 2007 Send Buffers The application gives the TCP layer some data to send. The data is put in a send buffer, where it stays until the data is ACK’d.  it has to stay, as it might need to be sent again! The TCP layer won’t accept data from the application unless (or until) there is buffer space.

9 CSCE515 – Computer Network Programming 2007 Recv Buffers The received data is put in the recv buffer, where it stays until the application reads data The Recv Buffer won’t accept data from network unless (or until) there is buffer space, and the sequence number is within its sliding windows. 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

10 CSCE515 – Computer Network Programming 2007 ACKs A receiver doesn’t have to ACK every segment (it can ACK many segments with a single ACK segment). Each ACK can also contain outgoing data (piggybacking). If a sender doesn’t get an ACK after some time limit (MSL) it resends the data.

11 CSCE515 – Computer Network Programming 2007 TCP Flow Control Sender Application does a 2K write Application reads 2k Sender is blocked 2K SEQ=0 empty receiver 0 4K recv’s buffer 2K ACK = 2048 WIN = 2048 Application does a 3K write 2K SEQ=2048 Full ACK = 4096 WIN = 0ACK = 4096 WIN = 2048 1k SEQ=4096 Sender may send up to 2k 2K1K2K

12 CSCE515 – Computer Network Programming 2007 TCP Segment Order Most TCP implementations will accept out-of- order segments (if there is room in the buffer). Once the missing segments arrive, a single ACK can be sent for the whole thing. Remember: IP delivers TCP segments, and IP in not reliable - IP datagrams can be lost or arrive out of order.

13 CSCE515 – Computer Network Programming 2007 Termination 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.

14 CSCE515 – Computer Network Programming 2007 FIN 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.

15 CSCE515 – Computer Network Programming 2007 App1 App2 FIN SN=X FIN SN=X 1 ACK=X+1 2 ACK=Y+1 4 FIN SN=Y FIN SN=Y 3... “I have no more data for you” “ OK, I understand you are done sending.” Release connection? “OK - Now I’m also done sending data”. “Over and Out, Goodbye” Release connection? TCP Termination Release connection?

16 CSCE515 – Computer Network Programming 2007 App1 App2 FIN SN=X FIN SN=X 1 ACK=X+1 2 ACK=Y+1 4 FIN SN=Y FIN SN=Y 3... “I have no more data for you” FIN_WAIT_1 “ OK, I understand you are done sending.” CLOSE_WAIT “OK - Now I’m also done sending data”. LAST_ACK “Over and Out, Goodbye” TIME_WAIT TCP Termination FIN_WAIT_2 CLOSED

17 CSCE515 – Computer Network Programming 2007 TCP TIME_WAIT Once a TCP connection has been terminated (the last ACK sent) there is some unfinished business:  What if the ACK is lost? The last FIN will be resent and it must be ACK’d.  What if there are lost or duplicated segments that finally reach the destination after a long delay? TCP hangs out for a while to handle these situations.

18 CSCE515 – Computer Network Programming 2007 Test Questions Why is a 3-way handshake necessary? Who sends the first FIN - the server or the client? Once the connection is established, what is the difference between the operation of the server’s TCP layer and the client’s TCP layer? What happens if a bad guy can guess ISNs?

19 CSCE515 – Computer Network Programming 2007 TCP Sockets Programming Creating a passive mode (server) socket. Establishing an application-level connection. send/receive data. Terminating a connection.

20 CSCE515 – Computer Network Programming 2007 Client-Server Communication (TCP) socket() bind() listen() accept() read() write() read() close() socket() connect() write() read() close() TCP Client TCP Server well-known port blocks until connection from client process request connection establishment data(request) data(reply) end-of-file notification int socket(int family, int type, int protocol); int bind(int sockfd, struct sockaddr *my_addr, int addrlen); int listen(int sockfd, int backlog); int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); int accept(int sockfd, void *addr, int *addrlen); int close(int sockfd); int socket(int family, int type, int protocol);

21 CSCE515 – Computer Network Programming 2007 Creating a TCP socket int socket(int family,int type,int proto); int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ }

22 CSCE515 – Computer Network Programming 2007 Binding to well known address int mysock; struct sockaddr_in myaddr; mysock = socket(PF_INET,SOCK_STREAM,0); myaddr.sin_family = AF_INET; myaddr.sin_port = htons( 80 ); myaddr.sin_addr = htonl( INADDR_ANY ); bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));

23 CSCE515 – Computer Network Programming 2007 Establishing a passive mode TCP socket Passive mode:  Address already determined.  Tell the kernel to accept incoming connection requests directed at the socket address. 3-way handshake  Tell the kernel to queue incoming connections for us.

24 CSCE515 – Computer Network Programming 2007 listen() int listen( int sockfd, int backlog); sockfd is the TCP socket (already bound to an address) backlog is the number of incoming connections the kernel should be able to keep track of (queue for us). listen() returns -1 on error (otherwise 0).

25 CSCE515 – Computer Network Programming 2007 listen() Server TCP 3-way handshake complete accept arriving SYN Completed connection queue Incomplete connection queue Sum of both queues cannot exceed backlog

26 CSCE515 – Computer Network Programming 2007 Accepting an incoming connection. Once we call listen(), the O.S. will queue incoming connections  Handles the 3-way handshake  Queues up multiple connections. When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.

27 CSCE515 – Computer Network Programming 2007 accept() int accept( int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen); sockfd is the passive mode TCP socket. cliaddr is a pointer to allocated space. addrlen is a value-result argument  must be set to the size of cliaddr  on return, will be set to be the number of used bytes in cliaddr.

28 CSCE515 – Computer Network Programming 2007 accept() return value accept() returns a new socket descriptor (small positive integer) or -1 on error. After accept returns a new socket descriptor, I/O can be done using the read() and write() system calls. read() and write() operate a little differently on sockets (vs. file operation)!

29 CSCE515 – Computer Network Programming 2007 Terminating a TCP connection Either end of the connection can call the close() system call. If the other end has closed the connection, and there is no buffered data, reading from a TCP socket returns 0 to indicate EOF.

30 CSCE515 – Computer Network Programming 2007 Client Code TCP clients can call connect() which:  takes care of establishing an endpoint address for the client socket. don’t need to call bind first, the O.S. will take care of assigning the local endpoint address (TCP port number, IP address).  Attempts to establish a connection to the specified server. 3-way handshake

31 CSCE515 – Computer Network Programming 2007 connect() int connect( int sockfd, const struct sockaddr *server, socklen_t addrlen); sockfd is an already created TCP socket. server contains the address of the server (IP Address and TCP port number) connect() returns 0 if OK, -1 on error

32 CSCE515 – Computer Network Programming 2007 Reading from a TCP socket int read( int fd, char *buf, int max); By default read() will block until data is available. reading from a TCP socket may return less than max bytes (whatever is available). You must be prepared to read data 1 byte at a time!

33 CSCE515 – Computer Network Programming 2007 Writing to a TCP socket int write( int fd, char *buf, int num); write might not be able to write all num bytes (on a nonblocking socket). The book includes readn(), writen() and readline() function definitions.

34 CSCE515 – Computer Network Programming 2007 fork() In Unix the way to create a new process is the fork() system call. fork() is called once but it returns twice Return value:  0: return in the child  Non-0: the PID of the newly created process

35 CSCE515 – Computer Network Programming 2007 Client/Server before call to accept return Client (129.1.1.200)server connect() listenfd connection request 129.1.1.200:1500 65.1.1.200 *:80 listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd);

36 CSCE515 – Computer Network Programming 2007 Client/Server After call to accept return Client (129.1.1.200)server connect() listenfd connection request {129.1.1.200:1500, 65.1.1.200:80} 65.1.1.200 *:80 connfd listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd);

37 CSCE515 – Computer Network Programming 2007 Client/Server After call to accept return Client (129.1.1.200)Server (parent) connect() listenfd connection request {129.1.1.200:1500, 65.1.1.200:80} 65.1.1.200 *:80 connfd listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd); listenfd *:80 connfd Server (child) fork

38 CSCE515 – Computer Network Programming 2007 Client/Server After call to accept return Client (129.1.1.200)Server (parent) connect() listenfd connection {129.1.1.200:1500, 65.1.1.200:80} 65.1.1.200 *:80 listenfd=socket(…) bind(listenfd…) listen(listenfd,LISTENQ); For( ; ;) { connfd = accept(listenfd, …); if ( (pid = fork())==0) { close(listendf); doit(connfd); close(connfd); exit(0); } close(connfd); listenfd *:80 connfd Server (child)


Download ppt "CSCE 515: Computer Network Programming ------ TCP Details Wenyuan Xu Department of Computer Science and Engineering."

Similar presentations


Ads by Google