Unit 6
Transport Layer
Q1) Explain the transport layer primitives?
A1)
A service is specified by a set of primitives. A primitive means operation. To access the service a user process can access these primitives. These primitives are different for connection- oriented service and connectionless service.
There are five types of service primitives:
Q2) Explain UDP?
A2)
Q3) Explain process to process communication?
A3)
There are several ways to obtain process-to-process communication but the most common is through the client/server paradigm. A process on the local host, called a client needs services from a process usually on the remote hostcalled a server.
Both processes the client and server have the same name. For example, to get the day and time from a remote machine, we need a Daytime client process running on the local host and a Daytime server process running on a remote machine.
A remote computer can run several server programs at the same time, just as local computers can run one or more client programs at the same time. For communication, we must define the following:
1. Local host
2. Local process
3. Remote host
4. Remote process
User Datagram Format
The user datagram has a 16-byte header which is shown below:
Where,
Q4) Explain the operation and uses of UDP?
A4)
The UDP takes the data from the higher layer protocols and places them in the UDP messages. This is then passed down to the internet protocol for transmission. It is a simple form of TCP/IP.
Some of the steps that are followed in transmission that is UDP based are as follows:
Uses of UDP:
Q5) Explain TCP services?
A5)
TCP Services
The services offered by TCP to the processes at the application layer are as follows:
Process-to-Process Communication:
TCP provides process-to-process communication using port numbers.
Stream delivery services:
TCP is a stream-oriented protocol which allows the sending process to deliver data as a stream of bytes and allows the receiving process to obtain data as a stream of bytes.
TCP creates an environment in which the two processes seem to be connected by an imaginary “tube” that carries their data across the internet.
This imaginary environment is depicted in the following figure. The sending process produces (writes to) the stream of bytes, and the receiving process consumes (reads from) them.
Sending and Receiving Buffers:
Because sending and receiving processes do not read or write data at the same speed TCP needs buffers for storage.
There are two bufferssending buffer and the receiving buffer, one for each direction and these buffers are also necessary for flow and error control mechanisms used by TCP.
One way to implement a buffer is to use a circular array of 1-byte locations.
Segments:
The buffering handles the disparity between the speed of the producing and consuming process where we need one more step before we can send data.
The IP layer is a service provider for TCP that needs to send data in packets, not as a stream of bytes.
At the transport layer, TCP groups number of bytes together into a packet called segment. TCP adds header to each segment and delivers the segment to the IP for transmission.
The segment is encapsulated in IP datagram and transmitted. This entire operation is transparent to the receiving process.
The segments may be received out of order, lost, or corrupted and resent. All these are handled by TCP with the receiving process unaware of any activities.
Q6) Explain TCP features?
A6)
Features
Q7) Explain full duplex communication?
A7)
TCP offers full-duplex servicein which data can flow in both directions at the same time. Each TCP has a sending and receiving buffer and segments move in both directions.
Connection-oriented service:
TCP, unlike UDP, is a connection-oriented protocol. When a process at the site wants to send and receive data from another process at site B, the following occurs:
The TCP segment is encapsulated in an IP datagram and can be sent out of order, or lost, or corrupted, and then resent. Each may use a different path to reach the destination. There is no physical connection. TCP creates a stream-oriented environment which accepts the responsibility of delivering the bytes in order to the other site.
Reliable Service
TCP is a reliable transport protocol. It uses an acknowledgment mechanism to check the safe and sound arrival of data.
Q8) Explain TCP segment format?
A8)
TCP segment format:
The TCP segment consists of header fields and a data field. The data field contains a chunk of application data. The MSS (Maximum Segment Size) limits the maximum size of a segment’s data field.
When TCP sends a large file, suchan image as part of a web page, it typically breaks the file into chunks of size MSS.
Interactive applicationstransmit data chunks that are smaller than the MSSfor example, with remote login applications like Telnet.
The data field in the TCP segment is often only one byte. Because the TCP header is typically 20 bytes segments sent by Telnet may be only 21 bytes in length.
The figure shows the structure of the TCP segment. The header includes source and destination port numbers, which are used for multiplexing/demultiplexing data from/to upper-layer applications.
The header includes a checksum field. A TCP segment header also contains the following fields:
Q9) Explain TCP connections ?
A9)
TCP also introduces the terms "server" and "client", the server being the peer that listen()s for an incoming connection.
Q10) Explain TCP timers?
A10)
TCP timers
TCP uses several timers to ensure that excessive delays are not encountered during communications.
TCP implementation uses four timers –
RTT are of three types –
Initially -> No value
After first measurement -> RTTS = RTTm
After each measurement -> RTTS = (1-t) * RTTS + t * RTTm
Initially -> No value
After first measurement ->RTTd = RTTm/2
After each measurement ->RTTd = (1-k) * RTTd + k * (RTTm – RTTs)
Note : k=1/4 (default if not given)
Retransmission Timeout : RTO calculation – The value of RTO is based on the smoothed round-trip time and its deviation. Most implementations use the following formula to calculate the RTO:
Initial value -> Original
After any measurement -> RTO = RTTs + 4 * RTTd
RTO(new) = RTO(before retransmission) *2 )
2.Persistent Timer – To deal with a zero-window-size deadlock situation, TCP uses a persistence timer.When sending TCP receives an acknowledgment with a window size of zero, it starts a persistence timer.
When the persistence timer goes off, the sending TCP sends a special segment called a probe.
This segment contains only 1 byte of new data. It has a sequence number, but its sequence number is never acknowledged.
The probe causes the receiving TCP to resend the acknowledgment which was lost.
3.Keep Alive Timer – A keepalive timer is used to prevent a long idle connection between two TCPs. If a client opens a TCP connection to a server transfers some data and becomes silent the client will crash.
In this case, the connection remains open forever. So a keepalive timer is used. Each time the server hears from a client, it resets this timer. The time-out is usually 2 hours. If the server does not hear from the client after 2 hours, it sends a probe segment. If there is no response after 10 probes, each of which is 75 s apart, it assumes that the client is down and terminates the connection.
Time Wait Timer – This timer is used during tcp connection termination. The timer starts after sending the last Ack for 2nd FIN and closing the connection.
The quiet timer is usually set to twice the maximum segment lifetime ensuring that all segments still heading for the port have been discarded.
Q11) Explain Berkeley Sockets?
A11)
Socket Addresses:
Sockets provide a standard interface between network and application
There are two types of socket:
• Stream – provides a virtual circuit service
• Datagram – delivers individual packets
• Commonly used with TCP/IP and UDP/IP, but not specific to the Internet protocols
For connection between client and server
For creating a socket
Create an unbound socket which is not connected to network and can be used as either a client or a server
For specifying Addresses & Ports we
• Must specify the address and port when calling bind() or connect()
• The address can be either IPv4 or IPv6
• It could be modelled in C as a union, but the designers of the sockets API chose to use a number of structs.
The Addresses is specified using struct sockaddr
• It has a data field big enough to hold the largest address of any family
• Plus sa_len and sa_familyare used to specify the length and type of the address
• Treats the address as an opaque binary string
For struct sockaddr_in
• Two variations exist for IPv4 and IPv6 addresses
• Use struct sockaddr_in to hold an IPv4 address
• Has the same size and memory layout as struct sockaddr, but interprets the bits differently to give structure to the address.
For struct sockaddr_in6
• Two variations exist for IPv4 and IPv6 addresses
• Use struct sockaddr_in6 to hold an IPv6 address
• It has the same size and memory layout as struct sockaddr, but interprets the bits differently to give structure to the address
To work with either struct sockaddr_in or struct sockaddr_in6
• Cast it to a struct sockaddr before calling the socket routines
struct sockaddr_inaddr; ...
// Fill in addr here ...
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
{ ...
Q12) Explain Elementary System calls?
A12)
When a socket is created, it is allocated a slot in the file descriptor table the same way a file is.
Once this socket is associated with a file, we can do I/O such as read and write.
sockets are for network communication and network I/O deals with a completely different set of problems than file UO, but wherever possible, actions corresponding to file UO are accomplished.
The socket system calls is used by any process to create a socket for doing any network I/O.
Connect System Call Generally "connect" function is used by client program to establish a connection to a remote machine (server).
Syntax of connect is given below:
Listen() is executed after both socket() and bind() calls and before accept()
After listen system call accept() completes the process of establishing connection between the server and the client.
Q13) Explain TCP client server programs?
A13)
To create connection between client and server using TCP it has few functionalities like, TCP is suited for applications that require high reliability, and transmission time is relatively less critical.
The entire process can be broken down into following steps:
The entire process can be broken down into following steps:
TCP Server –
TCP Client –
Program:
#include <stdio.h> #include <netdb.h> #include <netinet/in.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #define MAX 80 #define PORT 8080 #define SA struct sockaddr
// Function designed for chat between client and server. voidfunc(intsockfd) { charbuff[MAX]; intn; // infinite loop for chat for(;;) { bzero(buff, MAX);
// read the message from client and copy it in buffer read(sockfd, buff, sizeof(buff)); // print buffer which contains the client contents printf("From client: %s\t To client : ", buff); bzero(buff, MAX); n = 0; // copy server message in the buffer while((buff[n++] = getchar()) != '\n') ;
// and send that buffer to client write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat ended. if(strncmp("exit", buff, 4) == 0) { printf("Server Exit...\n"); break; } } }
// Driver function intmain() { intsockfd, connfd, len; structsockaddr_inservaddr, cli;
// socket create and verification sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd == -1) { printf("socket creation failed...\n"); exit(0); } else printf("Socket successfully created..\n"); bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification if((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { printf("socket bind failed...\n"); exit(0); } else printf("Socket successfully binded..\n");
// Now server is ready to listen and verification if((listen(sockfd, 5)) != 0) { printf("Listen failed...\n"); exit(0); } else printf("Server listening..\n"); len = sizeof(cli);
// Accept the data packet from client and verification connfd = accept(sockfd, (SA*)&cli, &len); if(connfd< 0) { printf("server acccept failed...\n"); exit(0); } else printf("server acccept the client...\n");
// Function for chatting between client and server func(connfd);
// After chatting close the socket close(sockfd); } |
Compilation –
Server side:
gccserver.c -o server
./server
Client side:
gccclient.c -o client
./client
Output
Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...
Client side:
Socket successfully created..
connected to the server..
Enter the string : hi
From Server : hello
Enter the string : exit
From Server : exit
Client Exit...
Q14) Explain UDP client server programs?
A14)
In UDP, the client does not form a connection with the server instead, it sends a datagram. Similarly, the server need not to accept a connection and just waits for datagrams to arrive.
We can call a function called connect() in UDP it just checks for any immediate errors and store the peer’s IP address and port number
. connect() is storing peers address so no need to pass server address and server address length arguments in sendto().
Necessary Functions :
int connect(int sockfd, const struct sockaddr *servaddr,
socklen_taddrlen);
returns : 0 if OK -1 on error
arguments :
sockfd : File descriptor of socket to be connected.
struct sockaddr *servaddr : server address structure.
addrlen : length of server address structure.
This shows the message transfer between server-client :
UDP Server code :
// server program for udp connection #include <stdio.h> #include <strings.h> #include <sys/types.h> #include <arpa/inet.h> #include <sys/socket.h> #include<netinet/in.h> #define PORT 5000 #define MAXLINE 1000
// Driver code intmain() { charbuffer[100]; char*message = "Hello Client"; intlistenfd, len; structsockaddr_inservaddr, cliaddr; bzero(&servaddr, sizeof(servaddr));
// Create a UDP Socket listenfd = socket(AF_INET, SOCK_DGRAM, 0); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); servaddr.sin_family = AF_INET;
// bind server address to socket descriptor bind(listenfd, (structsockaddr*)&servaddr, sizeof(servaddr));
//receive the datagram len = sizeof(cliaddr); intn = recvfrom(listenfd, buffer, sizeof(buffer), 0, (structsockaddr*)&cliaddr,&len); //receive message from server buffer[n] = '\0'; puts(buffer);
// send the response sendto(listenfd, message, MAXLINE, 0, (structsockaddr*)&cliaddr, sizeof(cliaddr)); } |
UDP Client code :
// udp client driver program #include <stdio.h> #include <strings.h> #include <sys/types.h> #include <arpa/inet.h> #include <sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<stdlib.h>
#define PORT 5000 #define MAXLINE 1000
// Driver code intmain() { charbuffer[100]; char*message = "Hello Server"; intsockfd, n; structsockaddr_inservaddr;
// clear servaddr bzero(&servaddr, sizeof(servaddr)); servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); servaddr.sin_port = htons(PORT); servaddr.sin_family = AF_INET;
// create datagram socket sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// connect to server if(connect(sockfd, (structsockaddr *)&servaddr, sizeof(servaddr)) < 0) { printf("\n Error : Connect Failed \n"); exit(0); }
// request to send datagram // no need to specify server address in sendto // connect stores the peers IP and port sendto(sockfd, message, MAXLINE, 0, (structsockaddr*)NULL, sizeof(servaddr));
// waiting for response recvfrom(sockfd, buffer, sizeof(buffer), 0, (structsockaddr*)NULL, NULL); puts(buffer); // close the descriptor close(sockfd); } |
Outputs:
Q15) Explain flow and error control in TCP?
A15)
Flow and error control in TCP
Error control in TCP is achieved through use of three simple techniques:
1. Checksum
2. Acknowledgement
3. Retransmission
i. When a retransmission timer expires
ii. When the sender receives three duplicate ACKs
i. TCP maintains one RTO timer for all outstanding (sent, but not acknowledged) segments.
ii. When the timer maturesthe earliest outstanding segment is retransmitted.
iii. Value of RTO is dynamic and updated based on RTT.
Flow control in TCP
TCP uses an end-to-end flow control protocol to avoid the sender send data too fast for the TCP receiver. It uses sliding window protocol.
In each TCP segment, the receiver specifies in the receive window field the amount of additionally received data that it is willing to buffer for connection.
The size of the window is determined by the lesser of two values: rwnd or cwnd
i. rwnd
It is the number of bytes the receiver can accept before its buffer overflows.
ii. cwnd
It is the value determined by the network to avoid congestion.