Tuesday, 27 January 2015

program on flow control by implementing Sliding window protocols.


Write a program on flow control by implementing Sliding window protocols.

//sliding window program-Selective Repeat ARQ
import java.io.*;
import java.math.*;
import java.util.*;

public class ntdd9
{
            public static void main(String s[])
            {
                        Scanner is=new Scanner(System.in);
                        int a[]=new int[20];
                        int b[]=new int[8];
                        Random r=new Random();
                        int winsize, pack;
                        boolean flag=true;
                        int x,y,z,w;
                        int seq=r.nextInt(500);
                        for(int i=0;i<20;i++)
                        {
                                    a[i]=seq;
                                    seq++;
                        }
                        System.out.println("Initially sender ");
                        for(int i=0;i<20;i++)
                        {
                                    System.out.println(a[i]);
                        }
                        System.out.println("Enter initially no of data receiver will receive");
                        int f=is.nextInt();
                        System.out.println("values in receiver ");
                                   
                        for(int j=0;j<f;j++)
                        {
                                    b[j]=a[j];
                                    System.out.println(b[j]);
                        }
                        int ack=b[f-1]+1;
                        winsize=b.length-f;
                        System.out.println("window size ="+winsize);
                        System.out.println("Ack ="+ack);
                        System.out.println("Seq =");
                        for(int i=0;i<winsize;i++)
                        {
                                    System.out.println(ack+" ");
                                    ack++;
                        }
                        System.out.println();
                                   
                        if(winsize==0)
                                    System.out.println("buffer is full, data must be deleted");
                        else
                        {
                                    for(int k=0;k<winsize;k++)
                                    {
                                                b[f]=a[f];
                                                f++;
                                    }
                                    System.out.println("receiver");
                                    for(int l=0;l<f;l++)
                                    {
                                                System.out.println(b[l]+"");
                                    }
                                    int m=f;
                                   
                                    outer:while(true && flag)
                                    {
                                                System.out.println("enter packets of data to be consumed by receiver ");
                                                int n=is.nextInt();
                                                int q=0;
                                                System.out.println("receiver after consumed");
                                                for(int i=n;i<f;i++)
                                                {
                                                            System.out.println(b[i]);
                                                            q++;
                                                }
                                                winsize=f-q;
                                                System.out.println("window size "+winsize);
                                                int ackl=a[m];
                                                System.out.println("ack="+ackl);
                                                for(int i=0;i<winsize;i++)
                                                {
                                                            System.out.println(ackl+" ");
                                                            ackl++;
                                                }
                                                System.out.println();
                                                if(winsize==0)
                                                            System.out.println("buffer is full, data must be deleted");
                                                int h=0;
                                                for(int i=0;i<n;i++)
                                                {
                                                            b[h]=a[m];
                                                            if(m<19)
                                                            {
                                                                        h++;
                                                                        m++;
                                                            }
                                                            else
                                                            {
                                                                        flag=false;
                                                                        break outer;
                                                            }
                                                }
                                                System.out.println("receiver after transfer :");
                                                for(int l=0;l<f;l++)
                                                {
                                                            System.out.println(b[l]);
                                                }
                                    }
                                    System.out.println("all the data is transfered");
                                    System.out.println("data at receiver after all data transfered");
                                    for(int l=0;l<f;l++)
                                    {
                                                System.out.println(b[l]);
                                    }
                        }
            }         
}
                                                           
/*output:
Initially sender
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
Enter initially no of data receiver will receive
5
values in receiver
319
320
321
322
323
window size =3
Ack =324
Seq =
324
325
326

receiver
319
320
321
322
323
324
325
326
enter packets of data to be consumed by receiver
6
receiver after consumed
325
326
window size 6
ack=327
327
328
329
330
331
332

receiver after transfer :
327
328
329
330
331
332
325
326
enter packets of data to be consumed by receiver
4
receiver after consumed
331
332
325
326
window size 4
ack=333
333
334
335
336

receiver after transfer :
333
334
335
336
331
332
325
326
enter packets of data to be consumed by receiver
5
receiver after consumed
332
325
326
window size 5
ack=337
337
338
339
340
341

all the data is transfered
data at receiver after all data transfered
337
338
335
336
331
332
325
326
Press any key to continue...

*/

No comments:

Post a Comment