TCP week 8 5/12/02 1 Clients & Servers • Client: ß in general, an application that initiates a peer-to-peer communication ß end users usually invoke client software • Server: ß waits for incoming communication requests from a client ß performs necessary computation and ß probably returns a result 5/12/02 2 Concurrent Vs. Iterative • concurrent-server ß handles multiple requests at one time • iterative-server ß process one request at a time 5/12/02 3 1
Connection - [oriented | less] • Connectionless ß UDP - User Datagram Protocol ß the burden of the data integrity is on the application • Connection-oriented ß TCP - Transport Control Protocol ß the program is free to deal with higher things 5/12/02 4 types of server/client iterative iterative connectionless connection-oriented concurrent concurrent connectionless connection-oriented 5/12/02 5 Server types • iterative, connectionless ß the most common • usually stateless • trivial amount of processing • iterative, connection-oriented ß less common • trivial amount of data but • need reliable transport 5/12/02 6 2
Server types … • concurrent, connectionless ß very uncommon • a process is created for each request • actually tftpd is one • concurrent, connection-oriented ß the most common • reliable transport • usually used by long living activities 5/12/02 7 TCP: Transmission Control Protocol • connection oriented ß exactly two end points. • no broadcast/multicast ß the two applications must establish a TCP connection with each other before they can exchange data. • reliable • byte stream ß 8-bit bytes with no interpretation ß there are no record boundaries 5/12/02 8 reliable … • data is broken up into best size chunks ß the unit of information passed by TCP to IP is called a segment • each segment sent has a timer ß when the timer expires before an acknowledgment is received, the segment is retransmitted • when data is received, an acknowledgment is sent ß but not immediately • the data and header have a checksum ß a segment with a bad/invalid checksum is dropped, the sender times out and retransmits 5/12/02 9 3
reliable … • preserves sequence ß IP datagrams can arrive out of order ß segments are resequenced if necessary • drops duplicates ß since IP datagrams can get duplicated. • flow control ß each end of a TCP connection has a finite amount of buffer space ß the receiving side allows the other end to send as much data as it has buffer for. 5/12/02 10 TCP Encapsulation IP datagram TCP segment IP TCP TCP data header header 5/12/02 11 TCP Header 16-bit source port number 16-bit destination port number 32-bit sequence number 32-bit acknowledgment number 20 4-bit header bytes 6-bit flags 16-bit window size length 16-bit TCP checksum 16-bit urgent pointer max. 60 bytes options (if any) data (if any) 5/12/02 12 4
TCP Header … • each segment contains a source and destination port number • together with the source and destination IP number from the IP header we get a unique identification of each connection • socket: IP address + port number • socket pair : source + destination sockets 5/12/02 13 TCP Header … • flags: ß URG the urgent pointer is valid ß ACK the acknowledgement number is valid ß PSH the receiver should pass this data ASAP ß RST Reset the connection ß SYN Synchronize sequence number to init. connection ß FIN the sender has finished sending data 5/12/02 14 Connection Establishment tree-way handshake: 1. the client sends a SYN segment specifying the port # of the server it wants to connect to, and the client’s ISN - initial sequence number 2. the server responds with its own SYN segment containing the server’s ISN. The server also ACKs the client’s SYN by ACKing the client’s ISN+1 3. the client must ACK this SYN from the server by ACKing the server’s ISN+1 5/12/02 15 5
State Transition Diagram Closed application: passive open application: active open send: nothing send: SYN Listen recv: SYN passive open send: SYN+ACK SYN_SENT SYN_RCVD active open recv: ACK recv: SYN+ACK send: nothing send: ACK Established Server data transfer state 5/12/02 16 Client Sliding window initial window 1 2 3 4 5 6 7 8 9 10 window slides 1 2 3 4 5 6 7 8 9 10 the sender can transmit all packets in the window without waiting for an acknowledgement 5/12/02 17 main(int cc, char **vv) { struct sockaddr_in srv; char buf[BUFSIZ], *addr; int sfd, n; bzero(&srv, sizeof(struct sockaddr_in)); addr = vv[1]; if((sfd = socket (PF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if(!isdigit(*addr)) { struct hostent *hp; if((hp = gethostbyname(addr)) == NULL) { fprintf(stderr, "host '%s': not found\n", addr); exit(1); } memcpy((caddr_t *)&srv.sin_addr, hp->h_addr_list[0], hp->h_length); } else srv.sin_addr.s_addr = inet_addr(addr); 5/12/02 18 6
srv.sin_family = AF_INET; srv.sin_port = htons(13); if( connect (sfd, (struct sockaddr *)&srv, sizeof(srv)) < 0) { perror("connect"); exit(1); } if( write (sfd, buf, 0) < 0) { perror("write"); exit(1); } n = read (sfd, buf, sizeof(buf)); if(n < 0) { perror("recvfrom"); exit(1); } printf("%.*s", n, buf); exit(0); } 5/12/02 19 7
Recommend
More recommend