Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Transport Layer Protocols TCP UDP SCTP

Similar presentations


Presentation on theme: "Chapter 2 Transport Layer Protocols TCP UDP SCTP"— Presentation transcript:

1 Chapter 2 Transport Layer Protocols TCP UDP SCTP
By Kulveer Singh

2 TCP SCTP UDP TCP reliable end to end delivery UDP unreliable
SCTP (Stream control transport protocol) ->used for internet telephony communication/signals. ->It supports multiple streams and MultiHoming ->What is MultiHoming? ->which allows a single SCTP endpoint to support multiple IP addresses

3 TCP Lingo When a client requests a connection it sends a “SYN” segment (a special TCP segment) to the server port. SYN stands for synchronize. The SYN message includes the client’s ISN. ISN is Initial Sequence Number.

4 More... Every TCP segment includes a Sequence Number that refers to the first byte of data included in the segment. Every TCP segment includes a Request Number (Acknowledgement Number) that indicates the byte number of the next data that is expected to be received. All byte up through this number have already been received.

5 And more... There are a bunch of control flags:
URG: urgent data included. ACK: this segment is (among other things) an acknowledgement. RST: error - abort the session. SYN: synchronize Sequence Numbers (setup) FIN: polite connection termination.

6 And more... MSS: Maximum segment size (A TCP option)
Window: Every ACK includes a Window field that tells the sender how many bytes it can send before the receiver will have to toss it away (due to fixed bugger size).

7 TCP Connection Creation
Programming details later - for now we are concerned with the actual communication. A server accepts a connection. Must be looking for new connections! A client requests a connection. Must know where the server is!

8 Client Starts A client starts by sending a SYN segment with the following information: Client’s ISN (generated pseudo-randomly) Maximum Receive Window for client. Optionally (but usually) MSS (largest datagram accepted). No payload! (Only TCP headers)

9 Sever Response When a waiting server sees a new connection request, the server sends back a SYN segment with: 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)

10 Finally When the Server’s SYN is received, the client sends back an ACK with: Request Number is Server’s ISN+1

11 Client Server SYN ISN=X 1 SYN ISN=Y ACK=X+1 2 ACK=Y+1 3

12 TCP 3-way handshake Client: “I want to talk, and I’m starting with byte number X”. Server: “OK, I’m here and I’ll talk. My first byte will be called number Y, and I know your first byte will be number X”. Client: “Got it - you start at byte number Y”. Bill: “Monica, I’m afraid I’ll syn and byte your ack” 1 2 3 ?

13 Why 3-Way? Why is the third message necessary? HINTS:
TCP is a reliable service. IP delivers each TCP segment. IP is not reliable.

14 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 segment (data or empty) includes a request number indicating what data has been received.

15 Buffering Keep in mind that TCP is part of the Operating System takes care of all these details asynchronously. The TCP layer doesn’t know when the application will ask for any received data. TCP buffers incoming data so it’s ready when we ask for it.

16 TCP Buffers Both the client and server allocate buffers to hold incoming and outgoing data The TCP layer does this. Both the client and server announce with every ACK how much buffer space remains (the Window field in a TCP segment).

17 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. The TCP layer won’t accept data from the application unless (or until) there is buffer space.

18 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.

19 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.

20 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.

21 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.

22 App1 App2 FIN SN=X 1 ACK=X+1 2 ... FIN SN=Y 3 ACK=Y+1 4

23 TCP Termination App1: “I have no more data for you”.
App2: “OK, I understand you are done sending.” dramatic pause… App2: “OK - Now I’m also done sending data”. App1: “Roger, Over and Out, Goodbye, Astalavista Baby, Adios, It’s been real ...” camera fades to black ... 1 2 3 4

24 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.

25 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? How many times did Bill: FIN? FIB? ACK his FIBs?

26 TCP Sockets Programming
Creating a passive mode (server) socket. Establishing an application-level connection. send/receive data. Terminating a connection.

27 Creating a TCP socket int sock; sock = socket( PF_INET, SOCK_STREAM,
int socket(int family,int type,int proto); int sock; sock = socket( PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ }

28 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, &myaddr, sizeof(myaddr));

29 Establishing a passive mode TCP socket
Address already determined. Tell the kernel to accept incoming connection requests directed at the socket address. Tell the kernel to queue incoming connections for us.

30 Listen() sockfd is the TCP socket (already bound to an address)
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. Listen() returns -1 on error (otherwise 0).

31 Accept() sockfd is the passive mode TCP socket.
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, will be set on return to be the number of bytes in cliaddr set by the call to accept.

32 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)!

33 Terminating a TCP connection
Either end of the connection can call the close() system call. If the other end had closed the connection and there is no buffered data, reading from a TCP socket returns EOF (0 bytes).

34 Client Code TCP clients can call connect() which:
takes care of establishing an endpoint address for the client socket (we don’t need to call bind). Attempts to establish a connection to the specified server.

35 connect() sockfd is an already created TCP socket.
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

36 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!

37 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).

38 Use Telnet client Putty
Download putty.exe telnet client from Login to SSH using the Server ip address and userid and password . Please mail me your userid and password if you want to have a login on Linux Machine to

39 Vi Editor Use any text editor to write a code in .c for C code and .h for header files . You can use Vi editor Common commands used for Vi vi: vi filename To exit vi and save changes: ZZ or :wq To exit vi without saving changes: :q! To enter vi command mode: [esc]

40 Compilation steps Cc -Wall –o –I/pathofIncludeFiles outputfilename filename.c gcc options -o output file name, (a.out if omitted) -Wall display warnings for many possible errors hello.c file to be compiled, can specify multiple .c files The info pages (info gcc) has much more information about gcc. These are in the gcc-doc package. To run ./outputfilename

41 Home Work Write a Client Server program in which Server(MathServer) will act like Calculator and client(mathClient) would be like User for example if you enter 2+ 5 on client console then server should return 7 Server will do the all mathematical processing and echo back the result to Client.

42 Thanks For any queries Please mail me at


Download ppt "Chapter 2 Transport Layer Protocols TCP UDP SCTP"

Similar presentations


Ads by Google