0% found this document useful (0 votes)
4 views59 pages

CN Module 6

The document provides an overview of Transport Layer Protocols, specifically focusing on Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). It outlines the characteristics, connection establishment, and termination processes for TCP, as well as the differences between TCP and UDP. Additionally, it discusses techniques for improving Quality of Service (QoS) and introduces socket programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views59 pages

CN Module 6

The document provides an overview of Transport Layer Protocols, specifically focusing on Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). It outlines the characteristics, connection establishment, and termination processes for TCP, as well as the differences between TCP and UDP. Additionally, it discusses techniques for improving Quality of Service (QoS) and introduces socket programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3/9/2023

Module: 6
Transport Layer Protocols

COMPUTER NETWORKS
By - Dr. A. Swaminathan, SCOPE, VIT Chennai

Dr. A. Swaminathan, VIT Chennai


Session Outline
 Transport Layer Protocols – An Introduction

 Transmission Control Protocol (TCP)

 User Datagram Protocol (UDP)

 Difference between TCP and UDP

 TCP Connection Establishment

 TCP Connection Termination


Transport Layer Protocols
 Layer 4 protocol (in OSI model)
 Provides Process-to-Process communication service
 Also called as end-to-end prototcol
 Performs Multiplexing and Demultiplexing
 Transport Layer Protocols
 Transmission Control Protocol (TCP)
 User Datagram Protocol (UDP)
 Runs over IP
 TCP and UDP packets are encapsulated into IP packets
 Use their own control information, stored in packet headers
 Port numbers (indicate consuming program in the destination host)
Transmission Control Protocol (TCP)

 Connection Oriented
 Reliable Byte Stream service
 Guaranteed in-order delivery
 Full-duplex
 Includes a flow-control mechanism
 Implements congestion-control mechanism
 Packets exchanges between TCP peers – Segments
 Each Segment has a header
Transmission Control Protocol (TCP)
TCP Header Format

*Source - Computer Networking: A Top Down Approach Featuring the Internet, [Link] and K.W.
Ross, Sixth Edition, Pearson Education, 2012

• Both the TCP header and data must have a length in bits multiple of 32
• {Source port, Source IP, Destination port, Destination IP} – uniquely identifies a TCP
Connection
TCP Header
Flags:
 SYN - establishing a TCP connection,
 FIN - terminating a TCP connection
 RST - close the connection
 ACK – acknowledgment
 PSH - push function
 URG - signifies that this segment contains urgent data. If set, the
urgent pointer field indicates the starting location of the nonurgent
data
User Datagram Protocol (UDP)
 Connectionless
 Unreliable
 Processes indirectly identify each other using port number (or
mailbox)
 UDP port field is only 16 bits long - there are up to 64K possible ports
 Source process sends a message to a port and destination process
receives the message from a port
 Client know about the server’s port either through
 Well known ports
 Port mapper
UDP Message Queue
UDP Header

*Source - Computer Networking: A Top Down Approach Featuring the Internet, [Link] and K.W.
Ross, Sixth Edition, Pearson Education, 2012

 UDP packet: No state information for the communication session.


 UDP is a stateless protocol, without re-transmission of loss data or protecting
against data recording
Difference between TCP & UDP
TCP UDP
Connection Oriented Connectionless
Byte stream Service Datagram Service
Reliable Unreliable
Inorder delivery of data Unordered
Guaranteed delivery Best effort service (no guarantee)
Implements Flow Control No flow control mechanism
Implements Congestion Control No such specific mechanism
Popular Internet applications and their
underlying transport Protocols

*Source - Computer Networking: A Top Down Approach Featuring the Internet, [Link] and K.W.
Ross, Sixth Edition, Pearson Education, 2012
TCP Connection Establishment
Steps involved in establishing TCP connection are
 The server must be ready to accept an incoming connection by
calling socket, bind, and listen - passive open
 The client issues an active open by calling connect.
 This initiates the Three way Handshake
1. Initially, the client TCP sends a "synchronize" (SYN) segment, which contains
the client's initial sequence number for the data to be sent on this connection
2. The server must acknowledge (ACK) the client's SYN and the server also
sends its own SYN in a single segment.
3. The client must acknowledge the server's SYN by sending ACK.
TCP Connection Establishment
Three way handshake
Client Server
Socket Socket, Bind, Listen
Connect (blocks) Accept (blocks)
SYN x
(active open) (passive open)
Segment1

SYN y, ACK x+1


Segment2
Connect returns
ACK y+1
Segment 3
Accept returns
read (blocks)

*Source - UNIX Network Programming: The Sockets Networking API, W. Richard Stevens, Volume 1,
Third Edition, Addison Wesley, 2003
TCP Connection Termination
 The server closes its connection with the client by calling close

 Steps involved in terminating TCP connection are


1. The client TCP sends a “finish" (FIN) segment

2. The server TCP acknowledges by sending ACK to the client

3. Then the server TCP sends a “finish" (FIN) segment for mutual termination

4. Finally, the client TCP acknowledges by sending ACK to the server


TCP Connection Termination
Four way teardown
Client Server
Close
FIN m Segment1
(passive close)
ACK m+1 Read returns 0
Segment2
Close
FIN n
Segment 3

ACK n+1
Segment 4

*Source - UNIX Network Programming: The Sockets Networking API, W. Richard Stevens, Volume 1,
Third Edition, Addison Wesley, 2003
Flow Characteristics

Flow
Characteristics

Reliability Delay Jitter Bandwidth


Techniques to improve QoS
 Scheduling
 Traffic Shaping
 Admission Control
 Resource Reservation
Scheduling
• Packets from different flows arrive at a router for processing

• Each router must implement some queuing discipline that


governs how packets are buffered while waiting to be transmitted

• A good scheduling technique treats the different flows in a fair and


appropriate manner
Scheduling
FIFO Queuing
• Packets wait in a buffer (queue) until the router is ready to process
them
• If the average arrival rate is higher than the average processing
rate, the queue will fill up and new packets will be discarded

FIFO queuing Tail drop at a FIFO queue


Scheduling
Priority Scheduling
 Simple variation on basic FIFO queuing
 Packets are first assigned to a priority class
 Each priority class has its own queue
 The packets in the highest-priority queue are processed first
 Packets in the lowest-priority queue are processed last
 A priority queue can provide better QoS than the FIFO queue
because higher priority traffic, such as multimedia, can reach the
destination with less delay
 Potential drawback- starvation
Scheduling
Fair Queuing
 The idea of FQ is to maintain a separate queue for
each flow currently being handled by the router
 The router then services these queues in a round-
robin fashion
 FQ also enforces fairness among a collection of
Fig: Round-robin service of four flows
flows

Packets with earlier finishing times are sent first sending of a packet already in progress is completed
Scheduling
Weighted Fair Queuing
 It allows a weight to be assigned to each flow (queue)
 This weight logically specifies how many bits to transmit each time
the router services that queue - effectively controls the percentage of
the link’s bandwidth that that flow will get
 The system processes packets in each queue in a round-robin
fashion with the number of packets selected from each queue based
on the corresponding weight
 For example, if the weights are 3, 2, and 1, three packets are
processed from the first queue, two from the second queue, and one
from the third queue
Traffic Shaping
 Traffic shaping is a mechanism to control
the amount and the rate of the traffic sent
to the network.
 Two techniques can shape traffic: leaky
bucket and token bucket

Leaky Bucket Algorithm


 A leaky bucket algorithm shapes bursty
traffic into fixed-rate traffic by averaging *Source - Data Communication and Networking, Behrouz A. Fourozan, McGraw Hill
Education, Fifth Edition, 2012
the data rate
 It may drop the packets if the bucket is
full
Traffic Shaping
Token bucket algorithm
 It allows idle hosts to accumulate credit for the future in the form of
tokens
 For each tick of the clock, the system sends n tokens to the bucket
 The system removes one token for every cell (or byte) of data sent
 The token bucket can easily be implemented with a counter
 The token is initialized to zero
 Each time a token is added, the counter is incremented by 1
 Each time a unit of data is sent, the counter is decremented by 1
 When the counter is zero, the host cannot send data
Traffic Shaping

*Source - Data Communication and Networking, Behrouz A. Fourozan, McGraw Hill Education, Fifth Edition, 2012
Admission Control

 Admission control refers to the mechanism used by a router, or a


switch, to accept or reject a flow based on predefined parameters
called flow specifications
 Before a router accepts a flow for processing, it checks the flow
specifications to see if its capacity (in terms of bandwidth, buffer size,
CPU speed, etc.) and its previous commitments to other flows can
handle the new flow
Resource Reservation
 A flow of data needs resources such as a buffer, bandwidth, CPU
time, and so on
 The quality of service is improved if these resources are reserved
beforehand
Sliding Window
 It guarantees the reliable delivery of data
 It ensures that data is delivered in order
 It ensures flow control between the sender and the receiver

To implement reliable and ordered delivery


 On the sending side, TCP maintains a send buffer which holds
 data that has been sent but not yet acknowledged
 data that has been written by the sending application but not transmitted
 On the receiving side, TCP maintains a receive buffer which holds
 data that arrives out of order
 data that is in the correct order but not yet read by the application process
Sliding Window
Sliding Window
 Three pointers are maintained into the send buffer: LastByteAcked, LastByteSent
and LastByteWritten. The following holds:

 LastByteAcked ≤ LastByteSent
 LastByteSent ≤ LastByteWritten

 A similar set of pointers are maintained on the receiving side: LastByteRead,


NextByteExpected and LastByteRcvd. The following holds:

 LastByteRead < NextByteExpected


 NextByteExpected ≤ LastByteRcvd+1
Sliding Window
To implement Flow Control
 TCP on the receive side must keep
 LastByteRcvd−LastByteRead ≤ MaxRcvBuffer

 It therefore advertises a window size of


 AdvertisedWindow =
MaxRcvBuffer−((NextByteExpected−1)−LastByteRead)
Sliding Window
 TCP on the send side must ensure that
 LastByteSent−LastByteAcked ≤ AdvertisedWindow

 The sender computes


 EffectiveWindow =
AdvertisedWindow−(LastByteSent−LastByteAcked)
Socket Programming
 What is Socket?
 Socket Programming API (C
language)
 Socket
 Bind
 Listen
 Accept
 Connect
 Send, Recv
 Close
 Example
What is a socket?
 An interface between application and network
 The application creates a socket
 The socket type dictates the style of communication
 reliable vs. best effort
 connection-oriented vs. connectionless

 Once configured, the application can


 pass data to the socket for network transmission
 receive data from the socket (transmitted through the network by some
other host)
Two essential types of sockets
 SOCK_STREAM  SOCK_DGRAM
 TCP sockets  UDP sockets
 reliable delivery  unreliable delivery
 in-order  no order guarantees
guaranteed  no notion of
 connection- “connection”
oriented We’ll look
at
App this one App D1

3 2 3 2
1
socket Dest.
1
socket D2

D3
Socket Creation
Interaction of Server – Client in UDP
socket( socket(
) )

bind( gethostname
) ()

Blocks until received


receive_from( send_to(
data (request)
) )

send_to( receive_from(
) )
Data reply

Close Close
() ()
Socket Creation in C: socket
int s = socket(domain, type,
protocol); where
 s: socket descriptor, an integer (like a file-handle)
 domain: integer, communication domain
 e.g., AF_INET (IPv4 protocol)
 Note. We’ll use AF_INET
 type: communication type
 SOCK_STREAM: reliable, 2-way, connection-based service
 SOCK_DGRAM: unreliable, connectionless
 Note. We’ll use SOCK_STREAM
 protocol: e.g., TCP or UDP
 use IPPROTO_TCP or IPPROTO_UDP to send/receive TCP or
UDP packets
 Note. We’ll use IPPROTO_TCP
Socket Creation in C: socket
/* sample code to create a socket */

hSocket=socket(AF_INET, SOCK_STREAM,
IPPROTO_TCP);
Binds a socket to an address
Addresses, Ports and Sockets
 Like apartments and mailboxes
 You are the application
 Street address of your apartment building is the IP
address
 Your mailbox is the port
 The post-office is the network
 The socket is the key that gives you access to the right
mailbox

 Q: How do you choose which port a socket connects


to?
Addresses, Ports and Sockets
 Choose a port number that is registered for general use, from
1024 to 49151
 Do not use ports 1 to 1023. These ports are reserved for use by
the Internet Assigned Numbers Authority (IANA)
 Avoid using ports 49152 through 65535. These are dynamic ports
that operating systems use randomly. If you choose one of these
ports, you risk a potential port conflict
The bind function
 associates a port for use by the socket
int status = bind(sock, & addrport, size)
Where

 status return status, 0 if successful, -1


: otherwise socket being used
 sock:
 addrport: address structure
 size: the size (in bytes) of the addr port
structure

Bind is non-blocking: returns immediately


The struct sockaddr
 The sockaddr_in structure has four parts:
 sin_family: address family (e.g., AF_INET IP
addresses)
 sin_port: port
 sin_addr number IP-
: address
 sin_zero // un-used
:
 #include <netinet/in.h> // All pointers to socket address structures are often
cast to pointers to this type before use in various functions and system calls:

struct sockaddr // IPv4 AF_INET sockets: struct in_addr


{ {
unsigned short struct sockaddr_in unsigned long s_addr;
sa_family; { // load with inet_pton()
// address family, AF_xxx short sin_family; };
// e.g. AF_INET, AF_INET6
char sa_data[14]; unsigned short sin_port;
// 14 bytes of // e.g. htons(3490)
protocol address struct in_addr sin_addr;
}; // see struct in_addr, below
char sin_zero[8];
// zero this if you want to
};
Example(Server)
// first, create and fill in values for the sockaddr_in structure
Address
struct sockaddr_in Address;
/* create Address stucture */
Address.sin_family = AF_INET;
/* AF_INET represents the address family INET for Internet sockets. */

Address.sin_port = htons(nHostPort);
/* The function htons() converts from host byte order to network byte
order*/

Address.sin_addr.s_addr = INADDR_ANY;
/* INADDR_ANY allows us to work without knowing the IP address of the
machine the client program is running on (very convenient) */

// next, bind the socket to the port


if( bind(hServerSocket, (struct sockaddr *) &Address, sizeof(Address)) == -1)
{
printf("\nCould not connect to host\n"); return -
1;
}
Connection setup
Connection setup: listen & accept
 The listen function prepares a bound socket to accept incoming connections
int status = listen(sock,
queuelen) where
 status return value, 0 if listening, -1 if
: error socket being used
 sock:
 queuelen: number of active participants that can “wait”
for a connection

 Example code:
if (listen(hServerSocket, 1) == -1)
{
printf("\nCould not
listen\n"); return -1;
}
Connection setup: listen & accept
 Use the accept function to accept a connection request from a
remote host
 The function returns a socket corresponding to the accepted
connection
 int s = accept(sock, &cliaddr, &addrlen)
– s: new socket used for data-transfer
– sock: original socket being listened on (e.g., server)
– cliaddr: address structure of the active participant (e.g., client)
 The accept function updates/returns the sockaddr structure with the
client's address information
– addrlen: size (in bytes) of the client sockaddr structure
 The accept function updates/returns this value
accept is blocking: waits for connection before returning
 Example code:
hSocket = accept(hServerSocket, (struct sockaddr *) &Cliaddr, (socklen_t *)
&AddressSize);
/* socklen_t is socket address length type, defined in sys/socket.h; in our example
code it is being cast from a pointer to an integer */
Client
create socket and connect to remote host
 First, the client must create a socket (socket call as before) and fills in its address
structure
 Then, the client connects to the remote host
 The connect function is used by a client program to establish communication with a remote
entity
 int status = connect(sock, &servaddr, addrlen);
where
 status: return value, 0 if successful connect, -1 otherwise
 sock: client’s socket to be used in connection
 servaddr: server’s address structure
 addrlen: size (in bytes) of the servaddr structure
connect is blocking
 Example code:
if(connect(hSocket, (struct sockaddr*) &Address, sizeof(Address))
== -1)
{
printf("\nCould not connect to host\n");
}
Sending/Receiving Data
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{int sd,sd2,nsd,clilen,sport,len,port;
char sendmsg[20],rcvmsg[20];
struct sockaddr_in servaddr, cliaddr;
printf("enter server port\n");
scanf("%d",&sport);
printf("%d\n",sport);
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("cant create\n");
else
printf("Socket is created\n");
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(sport);
sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(sd2<0)
printf("cant bind");
else
printf("Binded\n");
listen(sd,5);
clilen=sizeof(cliaddr);
nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);
if(nsd<0)
printf("cant accept");
else
printf("Accept");}
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{

 int csd,cport,len;
 char sendmsg[20],rcvmsg[20];
 struct sockaddr_in servaddr, cliaddr; printf("Enter
server port\n"); scanf("%d",&cport);
printf("%d\n",cport);
csd=socket(AF_INET,SOCK_STREAM,0); if(csd<0)
 printf("cant create\n"); else
 printf("Socket is created\n"); servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);//inet_addr("");
servaddr.sin_port=htons(cport);
 if(connect(csd,(struct
printf("Connected");
sockaddr*)&servaddr,sizeof(servaddr))<0) printf("cant
connect");
}
Sending / Receiving Data
 Send data
 int count = send(int s, const void * msg, int len, unsigned int
falgs);
Where:
 count
: number of bytes transmitted (-1 if error)
 sock: socket being used
 buf: buffer to be transmitted
 len: length of buffer (in bytes) to transmit
 flags: special options, usually just 0
 Receive data
int count = recv(int s, void *buf, int len, unsigned int
flags); Where:
 count number of bytes received (-1 if
: error)
 sock: socket being used
 buf: stores received bytes
 len: number of bytes received
 flags: special options, usually
just 0
Example (Client/Server)

// write a message to the server // after the client executed a write(), it will
n= read
send(sock,buffer,strlen(buffer),0) n = recv(newsock,buffer,255,0);
;

// do some
// do some processing processing
… …
// send the result to the client

// read a message from the


server n=
n = recv(sock,buffer,255,0); send(newsock,resp_msg,strlen(resp_msg)
,0);

CLIENT SERVER
close
 When finished using a socket, the socket should be
closed:
 status = close(s);
 status: return value, 0 if successful, -1 if error
 s: the file descriptor (socket being closed)
References
 Forouzan Behrouz, A. "Data Communication and networking." (2008).

 Peterson, Larry L., and Bruce S. Davie. Computer networks: a


systems approach. Elsevier, 2007.

 Stallings, William. Data and computer communications. Pearson Education


India, 2007.

 TCP/IP Protocol Suite, Behrouz A. Forouzan, McGraw-Hill Education, 4


Ed., 2010

 Web Links as mentioned in source

 [Link]
16/W/3214/CSE3214_10_PacketDelay_2016_posted.pdf

You might also like