OneCompiler

Sliding Window Protocol

1654

Program 4 Sender

 package prg4sender;
import java.net.*;
import java.rmi.*;
import java.io.*;
public class Prg4Sender{
public static void main(String []args)throws Exception{
ServerSocket ser=new ServerSocket(10);
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.println(“Enter the no.of frames:”);
Nf=Integer.parseInt(in.readLine());
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.println(“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.println(“\nDo you wants to send some 
more frames:”);
ch=in.readLine();
p.println(ch);
}
while(ch.equals(“yes”));
s.close();
 }
} 

Program 4 Receiver

 package prg6sender;
import java.net.*;
import java.io.*;
import java.rmi.*;
public class Prg6Receiver{
public static void main(String []args)throws Exception{
Socket s =new Socket(InetAddress.getLocalHost(),10);
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(“\nAcknowledgement sent\n”);
p.println(rptr+1);
rws+=nf;
}
else break;
ch=in.readLine();
}
while(ch.equals(“yes”));
}
}