Congestion Control
Aim: Write
a program for Congestion Control using :
a.
Stop & Wait protocol.
Apparatus
(Software): Eclipse/ Netbeans
Procedure:
Following should be studied to understand this practical
Congestion Control
Overview
Problem: When
too many packets are transmitted through a network, congestion occurs
. At very high traffic, performance collapses completely, and almost
no packets are delivered
Causes: Bursty
nature of traffic is the root cause → When part of the network no
longer can cope a sudden increase of traffic, congestion builds upon.
Other factors, such as lack of bandwidth, ill-configuration and slow
routers can also bring up congestion
Solution: congestion
control, and two basic approaches
– Open-loop: try to
prevent congestion occurring by good design
– Closed-loop:
monitor the system to detect congestion, pass this information to
where action can be taken, and adjust system operation to correct the
problem (detect, feedback and correct)
A. Stop & Wait
protocol.
In
this method of congestion and flow control, the sender sends a single
frame to receiver & waits for an acknowledgment.
•
The next frame is sent by sender only when acknowledgment of previous
frame is received.
•
This process of sending a frame & waiting for an acknowledgment
continues as long as the sender has data to send.
•
To end up the transmission sender transmits end of transmission (EOT)
frame.
•
The main advantage of stop & wait protocols is its accuracy. Next
frame is transmitted only when the first frame is acknowledged. So
there is no chance of frame being lost.
•
The main disadvantage of this method is that it is inefficient. It
makes the transmission process slow. In this method single frame
travels from source to destination and single acknowledgment travels
from destination to source. As a result each frame sent and received
uses the entire time needed to traverse the link. Moreover, if two
devices are distance apart, a lot of time is wasted waiting for ACKs
that leads to increase in total transmission time.
Program
A Java Program
implementing Stop and Wait Protocol while sending a message
travelling from sender to receiver.
//
SENDER SIDE
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.net.Socket;
public
class
Sender{
Socket
sender;
ObjectOutputStream
out;
ObjectInputStream
in;
String
packet,ack,str,
msg;
int
n,i=0,sequence=0;
Sender(){}
public
void
run(){
try{
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("Waiting
for Connection....");
sender
= new
Socket("localhost",2009);
sequence=0;
out=new
ObjectOutputStream(sender.getOutputStream());
out.flush();
in=new
ObjectInputStream(sender.getInputStream());
str=(String)in.readObject();
System.out.println("reciver
> "+str);
System.out.println("Enter
the data to send....");
packet=br.readLine();
n=packet.length();
do{
try{
if(i<n){
msg=String.valueOf(sequence);
msg=msg.concat(packet.substring(i,i+1));
}
else
if(i==n){
msg="end";out.writeObject(msg);break;
}
out.writeObject(msg);
sequence=(sequence==0)?1:0;
out.flush();
System.out.println("data
sent>"+msg);
ack=(String)in.readObject();
System.out.println("waiting
for ack.....\n\n");
if(ack.equals(String.valueOf(sequence))){
i++;
System.out.println("receiver
> "+"
packet recieved\n\n");
}
else{
System.out.println("Time
out resending data....\n\n");
sequence=(sequence==0)?1:0;
}
}catch(Exception
e){}
}while(i<n+1);
System.out.println("All
data sent. exiting.");
}catch(Exception
e){}
finally{
try{
in.close();
out.close();
sender.close();
}
catch(Exception
e){}
}
}
public
static
void
main(String args[]){
Sender
s=new
Sender();
s.run();
}
}
//
RECEIVER SIDE
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.net.ServerSocket;
import
java.net.Socket;
public
class
Reciever{
ServerSocket
reciever;
Socket
connection=null;
ObjectOutputStream
out;
ObjectInputStream
in;
String
packet,ack,data="";
int
i=0,sequence=0;
Reciever(){}
public
void
run(){
try{
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in));
reciever
= new
ServerSocket(2009,10);
System.out.println("waiting
for connection...");
connection=reciever.accept();
sequence=0;
System.out.println("Connection
established :");
out=new
ObjectOutputStream(connection.getOutputStream());
out.flush();
in=new
ObjectInputStream(connection.getInputStream());
out.writeObject("connected
.");
do{
try{
packet=(String)in.readObject();
if(Integer.valueOf(packet.substring(0,1))==sequence){
data+=packet.substring(1);
sequence=(sequence==0)?1:0;
System.out.println("\n\nreceiver
>"+packet);
}
else
{
System.out.println("\n\nreceiver
>"+packet
+"
duplicate data");
}
if(i<3){
out.writeObject(String.valueOf(sequence));i++;
}
else{
out.writeObject(String.valueOf((sequence+1)%2));
i=0;
}
}
catch(Exception
e){}
}while(!packet.equals("end"));
System.out.println("Data
recived="+data);
out.writeObject("connection
ended .");
}
catch(Exception
e){}
finally{
try{
in.close();
out.close();
reciever.close();
}
catch(Exception
e){}
}
}
public
static
void
main(String args[]){
Reciever
s=new
Reciever();
while(true){
s.run();
}
}
}
OUTPUT:
//SENDER OUTPUT
Waiting for Connection....
reciver > connected .
Enter the data to send....
myname
data sent>0m
waiting for ack.....
receiver > packet recieved
data sent>1y
waiting for ack.....
receiver > packet recieved
data sent>0n
waiting for ack.....
receiver > packet recieved
data sent>1a
waiting for ack.....
Time out resending data....
data sent>1a
waiting for ack.....
receiver > packet recieved
data sent>0m
waiting for ack.....
receiver > packet recieved
data sent>1e
waiting for ack.....
receiver > packet recieved
All data sent. exiting.
//RECEIVER OUTPUT
waiting for connection...
Connection established :
receiver >0m
receiver >1y
receiver >0n
receiver >1a
receiver >1a duplicate data
receiver >0m
receiver >1e
Data recived=myname
waiting for connection...
Sliding
window size 1. Sequence nos. 0 to 7.
B. Sliding Window
protocol.
A
sliding window protocol is a feature of packet-based data
transmission protocols. Sliding window protocols are used where
reliable in-order delivery of packets is required, such as in the
Data Link Layer (OSI model) as well as in the Transmission Control
Protocol (TCP).
- Frames have sequence number 0 to maximum 2n - 1 (n bit field).
- At any moment, the sender maintains a list of sequence numbers it is permitted to send - these fall within the sending window. These are frames sent-but-no-ack and frames not-yet-sent.
- When new packet from Network layer comes in to send, it is given highest no, and upper edge of window advanced by 1.
- When ack comes in, lower edge of window advanced by 1.
- Receiver has receiving window - the frames it is permitted to accept.
(a) At start. Receiver
waits for 0.
(b) Sender sends 0.
(c) Receiver receives
0. Waits for 1.
(d) Sender got ack for
0. Hasn't got 1 from its Network layer yet.
More complex Data Link
layer, as more freedom about the order in which it sends and receives
frames. Sender may have n
unacknowledged frames at any time (window size n).
Needs n buffers to hold
them all for possible re-transmit. If window grows to its
maximum size, DA must shut off NA. This is all hidden from
NB - still receives packets in exact same order.
1. Sender window
might grow as it receives more frames to send and still has ones
un-ack'ed.
Starts with nothing
to send, then NA gives it frames to send.
Later, window may
shrink as frames are ack-ed and NA has no more.
2. Receiver window
constant size.
Receiver window
size 1 means will only accept them in order.
Size n means will
receive out of order (e.g. receive later ones after earlier frame is
lost)
and then must
buffer them before sending to NB (must send to NB in order).
e.g. DB has
buffers to receive frames 0..7
Receives 1..7 in
varying orders. Still waiting on 0. Can't send frames to NB yet.
0 got lost and was
re-sent. Eventually gets 0.
Can now send all of
0..7 to NB
and re-use these slots.
e.g. consider
frames numbered 0..7 but DB only has 2 buffers
Currently the sliding
window is over 4,5
If get 4 can send it to
NB and move window to 5,6
If get 5 have to wait
for 4, then send both, and advance window to 6,7
Program
//CLIENT
PROGRAM
import
java.io.DataInputStream;
import
java.io.PrintStream;
import
java.net.ServerSocket;
import
java.net.Socket;
public
class
slidsender
{
public
static
void
main(String a[])throws
Exception
{
ServerSocket
ser=new
ServerSocket(7870);
Socket
s=ser.accept();
DataInputStream
in=new
DataInputStream(System.in);
DataInputStream
in1=new
DataInputStream(s.getInputStream());
String
sbuff[]=new
String[8];
PrintStream
p;
int
sptr=0,sws=8,nf,ano,i;
String
ch;
do
{
p=new
PrintStream(s.getOutputStream());
System.out.print("Enter
the no. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter
"+nf+"
Messages to be send\n");
for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.print("Acknowledgment
received");
ano=Integer.parseInt(in1.readLine());
System.out.println("
for "+ano+"
frames");
sws+=nf;
}
else
{
System.out.println("The
no. of frames exceeds window size");
break;
}
System.out.print("\nDo
you wants to send some more frames : ");
ch=in.readLine();
p.println(ch);
}
while(ch.equals("yes"));
s.close();
}
}
//RECEIVER
PROGRAM
import
java.io.DataInputStream;
import
java.io.PrintStream;
import
java.net.Socket;
class
slidreceiver
{
public
static
void
main(String a[])throws
Exception
{
Socket
s=new
Socket("localhost",7870);
DataInputStream
in=new
DataInputStream(s.getInputStream());
PrintStream
p=new
PrintStream(s.getOutputStream());
int
i=0,rptr=-1,nf,rws=8;
String
rbuf[]=new
String[8];
String
ch;
System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
{
for(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("The
received Frame "
+rptr+"
is : "+rbuf[rptr]);
}
rws-=nf;
System.out.println("\nAcknowledgment
sent\n");
p.println(rptr+1);
rws+=nf;
}
else
break;
ch=in.readLine();
}
while(ch.equals("yes"));
}
}
OUTPUT:
//SENDER OUTPUT
Enter the no. of frames : 4
Enter 4 Messages to be send
hiii
how r u
i am fine
how is evryone
Acknowledgment received for 4 frames
Do you wants to send some more frames :
no
//RECEIVER OUTPUT
The received Frame 0 is : hiii
The received Frame 1 is : how r u
The received Frame 2 is : i am fine
The received Frame 3 is : how is
evryone
Acknowledgment
sent
Comments
Post a Comment