0% found this document useful (0 votes)
57 views

Ex 1B Stop and Wait

The document describes the implementation of a stop-and-wait protocol between a sender and receiver program in Java. The sender splits messages into packets and sends them one by one, waiting for an acknowledgment after each. The receiver reassembles the full message from packets and sends acknowledgments. The programs establish a TCP connection, exchange packets and acknowledgments, and resend any lost packets before closing the connection.

Uploaded by

Navin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Ex 1B Stop and Wait

The document describes the implementation of a stop-and-wait protocol between a sender and receiver program in Java. The sender splits messages into packets and sends them one by one, waiting for an acknowledgment after each. The receiver reassembles the full message from packets and sends acknowledgments. The programs establish a TCP connection, exchange packets and acknowledgments, and resend any lost packets before closing the connection.

Uploaded by

Navin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF CSE
211416104185

IMPLEMENTATION OF STOP AND WAIT PROTOCOL

SENDER:

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(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");
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185

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:

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 .");
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
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:
RECEIVER:
waiting for connection...
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
211416104185
Connection established :
receiver >0a
receiver >1b
receiver >0c
receiver >1d
receiver >1d duplicate data
Data recived=abcd
SENDER:
Waiting for Connection....
reciver > connected .
Enter the data to send....
abcd
data sent>0a
waiting for ack.....

receiver > packet recieved

data sent>1b
waiting for ack.....

receiver > packet recieved

data sent>0c
waiting for ack.....

receiver > packet recieved

data sent>1d
waiting for ack.....

Time out resending data....

data sent>1d
waiting for ack.....

receiver > packet recieved

All data sent. exiting.

You might also like