Transmission Control Protocol TCP 1 TCP Overview RFCs

  • Slides: 29
Download presentation
Transmission Control Protocol (TCP) 1

Transmission Control Protocol (TCP) 1

TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 r point-to-point (unicast): m one sender,

TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 r point-to-point (unicast): m one sender, one receiver r connection-oriented: m handshaking (exchange of control msgs) init’s sender, receiver state before data exchange m State resides only at the END systems – Not a virtual circuit! r full duplex data: m bi-directional data flow in same connection (A->B & B->A in the same connection) m MSS: maximum segment size r reliable, in-order byte steam: m no “message boundaries” r send & receive buffers m buffer incoming & outgoing data r flow controlled: m sender will not overwhelm receiver r congestion controlled: m sender will not overwhelm network 2

TCP segment structure 32 bits URG: urgent data (generally not used) ACK: ACK #

TCP segment structure 32 bits URG: urgent data (generally not used) ACK: ACK # valid PSH: push data now (generally not used) RST, SYN, FIN: connection estab (setup, teardown commands) Internet checksum (as in UDP) source port # dest port # sequence number acknowledgement number head not UA P R S F len used checksum Receive window Urg data pnter Options (variable length) counting by bytes of data (not segments!) # bytes rcvr willing to accept application data (variable length) 3

TCP Connection-oriented demux r TCP socket identified by 4 -tuple: m source IP address

TCP Connection-oriented demux r TCP socket identified by 4 -tuple: m source IP address m source port number m dest IP address m dest port number r receiving host uses all four values to direct segment to appropriate socket 4

TCP Demultiplexing Example P P SP: 80 DP: 9157 client IP: A SP: 9157

TCP Demultiplexing Example P P SP: 80 DP: 9157 client IP: A SP: 9157 DP: 80 P 1 P P SP: 80 DP: 5775 server IP: C SP: 5775 DP: 80 Client IP: B 5

Typical TCP Transaction Client Server r A TCP Transaction consists of 3 Phases 1.

Typical TCP Transaction Client Server r A TCP Transaction consists of 3 Phases 1. Connection Establishment Reliable, In-Order Data Exchange Connection Establishment 1. Handshaking between client and server 2. Reliable, In-Order Data Exchange 1. Recover any lost data through retransmissions and ACKs Connection Termination 3. Connection Termination 1. Closing the connection time 6

TCP Connection Establishment r TCP sender, receiver establish “connection” before exchanging data segments r

TCP Connection Establishment r TCP sender, receiver establish “connection” before exchanging data segments r initialize TCP variables: m seq. #s m buffers, flow control info (e. g. Rcv. Window) r client: connection initiator Socket client. Socket = new Socket("hostname", port#); r server: contacted by client Socket connection. Socket = welcome. Socket. accept(); 7

Connection Establishment (cont) Three way handshake: Step 1: client host sends TCP SYN segment

Connection Establishment (cont) Three way handshake: Step 1: client host sends TCP SYN segment to server m specifies a random initial seq # m no data Step 2: server host receives SYN, replies with SYNACK segment server allocates buffers m specifies server initial seq. # Step 3: client receives SYNACK, replies with ACK segment, which may contain data m Host B Host A Connection request SYN, host ACKs and selects its own 3 4 initial seq # CK= A , 9 7 eq= S , CK A + SYN host ACKs ACK, time Seq= 43, A 42 CK=8 0 time Three-way handshake 8

Connection Establishment (cont) Host B Host A Connection request Seq. #’s: m byte stream

Connection Establishment (cont) Host B Host A Connection request Seq. #’s: m byte stream “number” of first byte in segment’s data ACKs: m seq # of next byte expected from other side m cumulative ACK SYN, host ACKs and selects its own 3 4 initial seq # CK= A , 9 7 eq= S , CK A + SYN host ACKs ACK, time Seq= 43, A 42 CK=8 0 time Three-way handshake 9

TCP Starting Sequence Number Selection r Why a random starting sequence #? Why not

TCP Starting Sequence Number Selection r Why a random starting sequence #? Why not simply choose 0? m m To protect against two incarnations of the same connection reusing the same sequence numbers too soon That is, while there is still a chance that a segment from an earlier incarnation of a connection will interfere with a later incarnation of the connection r How? m Client machine seq #0, initiates connection to server with seq #0. m Client sends one byte and client machine crashes m Client reboots and initiates connection again m Server thinks new incarnation is the same as old connection 10

TCP Connection Termination Closing a connection: client closes socket: client. Socket. close(); client close

TCP Connection Termination Closing a connection: client closes socket: client. Socket. close(); client close FIN Step 1: client end system sends TCP FIN control segment to server ACK Step 2: server receives server DATA ACK timed wait FIN, replies with ACK. Server might send some buffered but not sent data before closing the connection. Server then sends FIN and moves to closed Closing state. FIN ACK Data write close 11

TCP Connection Termination Step 3: client receives FIN, replies with ACK. m Enters “timed

TCP Connection Termination Step 3: client receives FIN, replies with ACK. m Enters “timed wait” - will respond with ACK to received FINs client closing server FIN Step 4: server, receives ACK. Connection closed. ACK r Why wait before closing the closing FIN connection? If the connection were allowed to move to CLOSED state, then another pair of application processes might come along and open the same connection (use the same port #s) and a delayed FIN from closed an earlier incarnation would terminate the connection. timed wait m ACK closed 12

TCP State-Transition Diagram CLOSED Active open Passive open /SYN Close LISTEN SYN/SYN + ACK

TCP State-Transition Diagram CLOSED Active open Passive open /SYN Close LISTEN SYN/SYN + ACK Send/ SYN/SYN + ACK SYN_RCVD ACK Close /FIN SYN + ACK/ACK ESTABLISHED Close /FIN FIN_WAIT_1 ACK SYN_SENT FIN/ACK CLOSE_WAIT AC K + FIN/ACK FI Close /FIN N FIN_WAIT_2 /A C K CLOSING ACK FIN/ACK TIME_WAIT LAST_ACK Timeout after two segment lifetimes ACK CLOSED 13

Typical TCP Client/Server Transitions TCP server lifecycle TCP client lifecycle 14

Typical TCP Client/Server Transitions TCP server lifecycle TCP client lifecycle 14

How to program using the TCP? Socket Layer TCP UDP IP LL PL Socket

How to program using the TCP? Socket Layer TCP UDP IP LL PL Socket Layer r Socket Layer: m Programmer’s API to the protocol stack TCP UDP IP LL PL r Typical network app has two pieces: client and server r Server: Passive entity. Provides service to clients m e. g. , Web server responds with the requested Web page r Client: initiates contact with server (“speaks first”) m typically requests service from server, e. g. , Web Browser 15

Socket Creation r my. Sock = socket(family, type, protocol); r UDP/TCP/IP-specific sockets Family TCP

Socket Creation r my. Sock = socket(family, type, protocol); r UDP/TCP/IP-specific sockets Family TCP UDP PF_INET Type Protocol SOCK_STREAM IPPROTO_TCP SOCK_DGRAM IPPROTO_UDP r Socket reference m File (socket) descriptor in UNIX m Socket handle in Win. Sock 16

TCP Client/Server Interaction Server starts by getting ready to receive client connections… 1. 2.

TCP Client/Server Interaction Server starts by getting ready to receive client connections… 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Assign a port to socket Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 17

TCP Client/Server Interaction /* Create socket for incoming connections */ if ((serv. Sock =

TCP Client/Server Interaction /* Create socket for incoming connections */ if ((serv. Sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) Die. With. Error("socket() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 18

TCP Client/Server Interaction echo. Serv. Addr. sin_family = AF_INET; /* Internet address family */

TCP Client/Server Interaction echo. Serv. Addr. sin_family = AF_INET; /* Internet address family */ echo. Serv. Addr. sin_addr. s_addr = htonl(INADDR_ANY); /* Any incoming interface */ echo. Serv. Addr. sin_port = htons(echo. Serv. Port); /* Local port */ if (bind(serv. Sock, (struct sockaddr *) &echo. Serv. Addr, sizeof(echo. Serv. Addr)) < 0) Die. With. Error("bind() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 19

TCP Client/Server Interaction /* Mark the socket so it will listen for incoming connections

TCP Client/Server Interaction /* Mark the socket so it will listen for incoming connections */ if (listen(serv. Sock, MAXPENDING) < 0) Die. With. Error("listen() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 20

TCP Client/Server Interaction for (; ; ) /* Run forever */ { clnt. Len

TCP Client/Server Interaction for (; ; ) /* Run forever */ { clnt. Len = sizeof(echo. Clnt. Addr); if ((clnt. Sock=accept(serv. Sock, (struct sockaddr *)&echo. Clnt. Addr, &clnt. Len)) < 0) Die. With. Error("accept() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 21

TCP Client/Server Interaction Server is now blocked waiting for connection from a client 1.

TCP Client/Server Interaction Server is now blocked waiting for connection from a client 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 22

TCP Client/Server Interaction Later, a client decides to talk to the server… 1. 2.

TCP Client/Server Interaction Later, a client decides to talk to the server… 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 23

TCP Client/Server Interaction /* Create a reliable, stream socket using TCP */ if ((sock

TCP Client/Server Interaction /* Create a reliable, stream socket using TCP */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) Die. With. Error("socket() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 24

TCP Client/Server Interaction echo. Serv. Addr. sin_family = AF_INET; /* Internet address family */

TCP Client/Server Interaction echo. Serv. Addr. sin_family = AF_INET; /* Internet address family */ echo. Serv. Addr. sin_addr. s_addr = inet_addr(serv. IP); /* Server IP address */ echo. Serv. Addr. sin_port = htons(echo. Serv. Port); /* Server port */ if (connect(sock, (struct sockaddr *) &echo. Serv. Addr, sizeof(echo. Serv. Addr)) < 0) Die. With. Error("connect() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 25

TCP Client/Server Interaction echo. String. Len = strlen(echo. String); /* Determine input length */

TCP Client/Server Interaction echo. String. Len = strlen(echo. String); /* Determine input length */ /* Send the string to the server */ if (send(sock, echo. String. Len, 0) != echo. String. Len) Die. With. Error("send() sent a different number of bytes than expected"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 26

TCP Client/Server Interaction /* Receive message from client */ if ((recv. Msg. Size =

TCP Client/Server Interaction /* Receive message from client */ if ((recv. Msg. Size = recv(clnt. Socket, echo. Buffer, RCVBUFSIZE, 0)) < 0) Die. With. Error("recv() failed"); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 27

TCP Client/Server Interaction close(sock); 1. 2. 3. 4. Client Create a TCP socket Establish

TCP Client/Server Interaction close(sock); 1. 2. 3. 4. Client Create a TCP socket Establish connection Communicate Close the connection close(clnt. Socket) 1. 2. 3. 4. Server Create a TCP socket Bind socket to a port Set socket to listen Repeatedly: a. Accept new connection b. Communicate c. Close the connection 28

TCP Tidbits n n Client knows server address and port No correlation between send()

TCP Tidbits n n Client knows server address and port No correlation between send() and recv() Client send(“Hello Bob”) Server recv() -> “Hello ” recv() -> “Bob” send(“Hi ”) send(“Jane”) recv() -> “Hi Jane” 29