DISHA NIRDESHAN

Enlightens your career

Wednesday, July 6, 2016

Selective Repeat - Java Implementation

/************* Selective Repeat Server ********* /


import java.io.*;
import java.net.*;
import java.util.LinkedList;
public class SelRserver {
    public static void main(String args[]){
        try {
            //Create Socket at Server Side
            ServerSocket s=new ServerSocket(95);
            Socket con =s.accept();
            System.out.println("Server Selective Repeat Started...");
           
            BufferedReader fromclient = new BufferedReader(new InputStreamReader(con.getInputStream()));
            DataOutputStream toclient= new DataOutputStream(con.getOutputStream());
            OutputStream output = con.getOutputStream();
           
            LinkedList list = new LinkedList();

            String f = fromclient.readLine();
           
            int nf=Integer.parseInt(f);
            System.out.println("Number of packets = "+nf);
           
            int lost_ack=0,rndnum=1,lost=0;
            String rndm=fromclient.readLine();
            rndnum= Integer.parseInt(rndm);
           
            String flow = fromclient.readLine();
       
           
            if(flow.contains("n") || flow.contains("N")){
                for(int i=0;i               
                    if(i==rndnum) {
                        System.out.println("lost packet is"+i);
                        list.add(i,"lost");
                        lost=i;
                        continue;
                    }
               
                    String n=fromclient.readLine();
               
                    int num=Integer.parseInt(n);
                    String packet=fromclient.readLine();
                    list.add(num,packet);
               
                    if(list.contains("lost")){
                   
                        lost_ack= list.indexOf("lost");
                        toclient.writeBytes(lost_ack+"\n");
                        num=lost_ack;
                   
               
                        if(i>rndnum){
                            System.out.println("sending ack"+i);
                            toclient.flush();
                            toclient.writeBytes(i+"\n");
                            toclient.flush();
                        }else
                        {
                            System.out.println("sending ack"+num);
                            toclient.flush();
                            toclient.writeBytes(num+"\n");
                            toclient.flush();
                        }
                    }else
                    {
                        System.out.println("sending ack"+num);
                        toclient.flush();
                        toclient.writeBytes(num+"\n");
                        toclient.flush();
                    }
                }
                System.out.println(list);
                String n=fromclient.readLine();
                int num=Integer.parseInt(n);
                String packet=fromclient.readLine();
                System.out.println("received packet"+num+" = "+packet);
                list.set(lost,packet);
                System.out.println(list);
                toclient.flush();
                int ack=num;
           
                //Send the acknowledgment for lost packet,once it is received from client again.
                if(list.contains("lost")) {
                    lost_ack= list.indexOf("lost");
                    System.out.println("Sending lost Ack"+lost_ack);
                    toclient.writeBytes(lost_ack+"\n");
                } else {
                    System.out.println("Sending Ack"+ack);
                    toclient.writeBytes(ack+"\n");
                }
            }else{
                for(int i=0;i                    String n=fromclient.readLine();
                    int num=Integer.parseInt(n);
                    String packet=fromclient.readLine();
                    list.add(num,packet);
                    System.out.println("received packet"+num+" = "+packet);
                    System.out.println("sending ack"+i);
                    toclient.flush();
                    toclient.writeBytes(i+"\n");
                    toclient.flush();
                }
                System.out.println("received the following packets"+list);
            }
            //Close connections with client
            fromclient.close();
            output.close();
            con.close();
            s.close();
        }catch (Exception e) {
            System.out.println("\n\n ERROR"+e);
        }
    }
}




/***** Selective Repeat Client *******/


import java.io.*;
import java.net.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.*;

public class SelRClient {
   
@SuppressWarnings("static-access")
    public static void main(String args[]) {
   
    long start = System.currentTimeMillis();
    try
    {
        //Create Socket at client side
        Socket SRClient =new Socket("localhost",95);
       
        //Receive the input from console
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream toSrServer= new DataOutputStream(SRClient.getOutputStream());
        BufferedReader fromSrServer = new BufferedReader(new InputStreamReader(SRClient.getInputStream()));
       
        //Create linked list to display and store the packets
        LinkedList list = new LinkedList();
       
        System.out.println("We maintained window size as 4,so for better performance evaluation kindly enter atleast 15 packets in input \n");
       
        Random rndNumbers = new Random();
        int rndNumber = 0;
       
        System.out.println("Enter number of packets to be transmitted");
       
        String n=in.readLine();
        int nf=Integer.parseInt(n);
        toSrServer.writeBytes(nf+"\n");
        int temp=nf-1;
       
        //Generate a random number so that particular packet is lost in demo
        rndNumber = rndNumbers.nextInt(temp);
       
        for(int i=0;i            System.out.println("Enter packet"+i+"to be transmitted");
            String packet=in.readLine();
            list.add(packet);
        }
        System.out.println("\nYou have entered following data "+list+"\n");
       
        toSrServer.writeBytes(rndNumber+"\n");
       
        System.out.println("Please enter Y for  successful sending or N for packet loss");
        String flow = in.readLine();
       
        toSrServer.writeBytes(flow+"\n");

         int w=4,wind=0,p=0,q=0;
        
        //While sending packet ignore the random number generated packet.
         if(flow.contains("n") || flow.contains("N")){
             for(int i=0;i                 p=i;
                 wind = windowSize(p,q);
           
                if((i==rndNumber)){
                   
                    System.out.println("Packet lost is"+i);
                    q=w;
                    wind = windowSize(p,q);
                    wind++;
                    q=wind;
                    continue;
                } else {
                    String str=list.get(i);
                    toSrServer.writeBytes(i+"\n");
                    toSrServer.writeBytes(str+"\n");
                }

                wind++;
                q=wind;
               
                String ak = fromSrServer.readLine();
                int ack = Integer.parseInt(ak);
                ack=i;
                System.out.println("Ack received for "+ack);
            }
                
            //Re-send the lost packet
            System.out.println("Resending packet"+rndNumber);
            String str=list.get(rndNumber);
       
            toSrServer.writeBytes(rndNumber+"\n");
            toSrServer.writeBytes(str+"\n");
            toSrServer.flush();
       
        //Timer set for expected acknowledgment
            Thread.currentThread().sleep(2000);

            String as = fromSrServer.readLine();
       
            System.out.println("Recieved ack no"+rndNumber);
            int aa = Integer.parseInt(as);
            if (aa != (list.size() -1))
                System.out.println("Ack received for "+rndNumber);
         }else
         {
             for(int i=0;i                p=i;
                 wind = windowSize(p,q);
                String str=list.get(i);
                 toSrServer.writeBytes(i+"\n");
                 toSrServer.writeBytes(str+"\n");
                 wind++;
                q=wind;
                 String ak = fromSrServer.readLine();
                int ack = Integer.parseInt(ak);
                ack=i;
                System.out.println("Ack received for "+ack);
             }
         }
        //Close socket and server connections
        fromSrServer.close();
        toSrServer.close();
        SRClient.close();
    }catch (Exception e){
        System.out.println("\n\n ERROR"+e);
    }
   
    //Calculate performance
    long end = System.currentTimeMillis();
    NumberFormat formatter = new DecimalFormat("#0.00000");
    System.out.println("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");

    }
    public static int windowSize(int p,int wind) throws InterruptedException
    {
        int i=p,w=i+4,windw=wind;
        LinkedList < Integer > list1 = new LinkedList < Integer > ();
        if(i==0 || windw==4){
            System.out.println("\nBased on the window size, packets to be sent ");
            for(int q=i;q                windw=0;
                list1.add(q);
           
            }
            System.out.println(list1);
            if(windw==0){
                Thread.currentThread();
                Thread.sleep(2000);
            }
        }
        return windw;
    }
}