/*Aim: A Java Program implementing Sliding Window Protocol while sending a message travelling from sender to receiver. */ /*For complete explanation visit: Prof. Brinda's Blog*/
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(); } } 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
No comments:
Post a Comment