Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transmission Control Protocol (TCP). TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 r reliable, in-order byte steam: m no “message boundaries” r send.

Similar presentations


Presentation on theme: "Transmission Control Protocol (TCP). TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 r reliable, in-order byte steam: m no “message boundaries” r send."— Presentation transcript:

1 Transmission Control Protocol (TCP)

2 TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 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 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

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

4 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

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

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

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

8 Connection Establishment (cont) Host A Host B SYN, Seq=42 SYN+ACK, Seq=79, ACK=43 ACK, Seq=43, ACK=80 time Three-way handshake 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 m server allocates buffers m specifies server initial seq. # Step 3: client receives SYNACK, replies with ACK segment, which may contain data time Connection request host ACKs and selects its own initial seq # host ACKs

9 Connection Establishment (cont) Host A Host B SYN, Seq=42 SYN+ACK, Seq=79, ACK=43 ACK, Seq=43, ACK=80 time Three-way handshake time Connection request host ACKs and selects its own initial seq # host ACKs 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

10 TCP Starting Sequence Number Selection r Why a random starting sequence #? Why not simply choose 0? m To protect against two incarnations of the same connection reusing the same sequence numbers too soon m 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

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

12 TCP Connection Termination Step 3: client receives FIN, replies with ACK. m Enters “timed wait” - will respond with ACK to received FINs Step 4: server, receives ACK. Connection closed. r Why wait before closing the connection? m 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 an earlier incarnation would terminate the connection. client FIN server ACK FIN closing closed timed wait closed

13 CLOSED LISTEN SYN_RCVDSYN_SENT ESTABLISHED CLOSE_WAIT LAST_ACKCLOSING TIME_WAIT FIN_WAIT_2 FIN_WAIT_1 Passive openClose Send/SYN SYN/SYN + ACK SYN + ACK/ACK SYN/SYN + ACK ACK Close/FIN FIN/ACKClose/FIN FIN/ACK ACK + FIN/ACK Timeout after two segment lifetimes FIN/ACK ACK Close/FIN Close CLOSED Active open/SYN TCP State-Transition Diagram

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

15 How to program using the TCP? TCP UDP IP LL PL Socket Layer TCP UDP IP LL PL Socket Layer TCP UDP IP LL PL Socket Layer r Socket Layer: m Programmer’s API to the protocol stack 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

16 Socket Creation FamilyType Protocol TCP PF_INET SOCK_STREAMIPPROTO_TCP UDPSOCK_DGRAMIPPROTO_UDP r mySock = socket(family, type, protocol); r UDP/TCP/IP-specific sockets r Socket reference m File (socket) descriptor in UNIX m Socket handle in WinSock

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

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

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

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

21 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Establish connection 3. Communicate 4. Close the connection Server 1. Create a TCP socket 2. Bind socket to a port 3. Set socket to listen 4. Repeatedly: a. Accept new connection b. Communicate c. Close the connection for (;;) /* Run forever */ { clntLen = sizeof(echoClntAddr); if ((clntSock=accept(servSock,(struct sockaddr *)&echoClntAddr,&clntLen)) < 0) DieWithError("accept() failed");

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

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

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

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

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

27 TCP Client/Server Interaction Client 1. Create a TCP socket 2. Establish connection 3. Communicate 4. Close the connection Server 1. Create a TCP socket 2. Bind socket to a port 3. Set socket to listen 4. Repeatedly: a. Accept new connection b. Communicate c. Close the connection /* Receive message from client */ if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0) DieWithError("recv() failed");

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

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


Download ppt "Transmission Control Protocol (TCP). TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 r reliable, in-order byte steam: m no “message boundaries” r send."

Similar presentations


Ads by Google