C Program For Stop And Wait Protocol

On

Implement the simplex protocol for a noisy channel (Fig. 3-12 from Tannenbaum).

Teacher to be able to implement the Stop-and-Wait Protocol which fulfills the basic need for network. Module content, your ability to program and/or teach programming, and how much available time you have in your. Python, Scratch, C, etc. False: Get a good frame. Stop and Wait Protocol. This is wrong, actually it is the. That is the reason why the program is getting printed when you try printing.

**General rules: source code must be commented and a description how to run the programs must be attached.**

The goal is to implement a data transmission programs that involve two layers:

Physical layer and Data link layer.

We will simulate the physical layer by the transport layer using the programs described in the lecture

'Unix Network Programming'.

For the programs on the data link layer follow the Fig. 3-12.

You will need to modify the meaning of functions in Fig. 3-12 so that they relate to the regular

Unix Network Programming functions.

Sender sends data every Pt = 500ms and always has data to send.

Sender timeout is Rt = 1 sec. Send packets over UDP.

Sender follows the usual logic.

We need to simulate losses since our network will almost never lose packet. At the receiver, write additional code in two places

1. As soon as the receives gets a packet (before any update to variables): Ignore the packet with probability P = 0.2. This simulates a lost data packet.

2. Just before the receiver sends an ack (after updating variables): Don't send the ack with probability P = 0.2. This simulates a lost ack.

**How to drop/ignore packet with probability P ?**

Generate a random float point number p, between 0 and 1. If p < P then drop/ignore packet, else handle packet.

Packet formatFirst byte : type (0 for data packet, 1 for ack)

Second byte: seqNo

Remaining bytes: 0

Output formatBoth sender and receiver must write log statement at a output file for every significant event. The log statement should have the following format:

<Timestamp>: <Event> <Event parameters>

For example, the sender log might look like the following:

19:32:31.234543: Sent_packet seqnum=0

19:32:31.234712: Received_ack seqnum=0

19:32:31.756743: Sent_packet seqnum=1

19:32:31.756943 Received_ack seqnum=1

19:32:31.756743: Sent_packet seqnum=0

19:32:32.845435: Timeout seqnum=0

19:32:32.845455: Sent_packet seqnum=0 Retransmit

19:32:32.845655: Received_ack seqnum=0

This shows what the sends log would like with a data packet is lost and timer expires.

Play around with with varying Pt, Rt and P values and see if the results are as expected. It is best to accept these values as command line parameters so that it is easy to experiment.

## Deliverables

1) Complete and fully-functional working program(s) in executable form as well as complete source code of all work done.

2) READ THE INSTRUCTIONS TO SEE WHAT NEEDS TO BE DONE, HOW THE OUTPUT IS TO BE SHOWED

I HAVE TWO SAMPLE CODES WITH ME FOR THE SAME ASSIGNMENT. IF U R READY TO DO THIS ASSIGNMENT, I CAN PROVIDE U WITH THAT TO HELP U MORE UNDERSTAND WHAT IS NEEDED.

## Platform

UNIX

C OR C++ IS OK.

Java Programming‎ > ‎Networking/Socket programming‎ > ‎

stop and wait

In this program
I used packet=sequencenumber+one character
it means the packet is of 2byte
In this programm the ack is destroy after 3 packet sent......
because in localhost i think no packet will destroy so to check it i do...........................
download programm run it..................
All the Best
Reciever

/*
Stop and wait protocol By pankaj kumar
this is reciever
send the packet of one character with its sequence number
*/
import java.io.*;
import java.net.*;
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(2004,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=(sequence0)?1:0;
System.out.println('nnreceiver >'+packet);
}else{
System.out.println('nnreceiver >'+packet +' duplicate data');
}
/*
when i3 then i destroy the ack
*/
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();
}
}
}

Sender

/*
Stop and wait protocol By pankaj kumar
this is sender
send the packet of one character with its sequence number
*/
import java.io.*;
import java.net.*;
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',2004);
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(in){
msg='end';out.writeObject(msg);break;
}
out.writeObject(msg);
/*
chenging sequence number since data sent
*/
sequence=(sequence0)?1:0;
out.flush();
System.out.println('data sent>'+msg);
ack=(String)in.readObject();
System.out.println('waiting for ack.....nn');
if(ack.equals(String.valueOf(sequence))){
i++;
System.out.println('receiver > '+' packet recievednn');
}
else{ /* whenever ack lost or wrong ack we change the sequence number
*/
System.out.println('Time out resending data....nn');
sequence=(sequence0)?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();
}
}

Reciever.java
Sender.java