Showing posts with label Stop and wait protocol. Show all posts
Showing posts with label Stop and wait protocol. Show all posts

Stop and Wait Protocol without Timeout


// Stop and Wait Protocol
// Sender

import java.io.*;
import java.net.*;

public class Sdr
{
     Socket sender;
   
     ObjectOutputStream out;
    ObjectInputStream in;
   
     String pkt, ack, data;
    
    int n, i = 0, seq = 0;
    
     Sdr()
     {
     }
   
     public void run() throws IOException
     {
        try
          {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           
              sender = new Socket("localhost",1500);
            seq = 0;

            out = new ObjectOutputStream(sender.getOutputStream());
            in = new ObjectInputStream(sender.getInputStream());
                       
            while(true)
              {
                System.out.print("\nEnter the data to send : ");
              data = br.readLine().trim();
                       
                   pkt = String.valueOf(seq);
                   pkt = pkt.concat(data);
                   out.writeObject(pkt);

                   out.flush();
                  
                   if(data.equals("Terminate"))
                        break;                       
                       
                   ack = (String)in.readObject();
                       
                   if(ack.equals(String.valueOf(seq)))
                   {
                        System.out.println("ack recerived. ");    
                        seq = (seq==0)?1:0;
                   }
                   else
                   {     
                        System.out.println("Time out resending data....\n\n");
                   }            
            }
          }
          catch(Exception e){}
       
          finally
          {
              in.close();
              out.close();
              sender.close();
              System.out.println("\nConnection Terminated.");
          }
    }
    
    public static void main(String args[]) throws IOException
     {
        Sdr s = new Sdr();
            s.run();
    }
}



// Stop and Wait Protocol
// Reciever

import java.io.*;
import java.net.*;

public class Rcr
{
    ServerSocket reciever;
    
    Socket conc = null;
   
     ObjectOutputStream out;
    ObjectInputStream in;
   
     String ack, pkt, data="";
    int i = 0, seq = 0;
  
     Rcr()
     {
     }
   
    
     public void run() throws IOException
     {
        try
          {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           
              reciever = new ServerSocket(1500,10);
              conc = reciever.accept();
           
              if(conc!=null)
                   System.out.println("Connection established :");
                            
              out = new ObjectOutputStream(conc.getOutputStream());
              in = new ObjectInputStream(conc.getInputStream());
             
              while(true)
              {
                   pkt = (String)in.readObject();
                   ack = pkt.substring(0,1);
                   data = pkt.substring(1);
                  
                   if(data.equals("Terminate"))
                        break;
                  
                   if(Integer.valueOf(ack) == seq)
                   {
                        System.out.println("\nMsg received : " + data);
                        out.writeObject(ack);
                        seq = (seq==0)?1:0;
                   }
                   else
                   {
                        System.out.println("\nMsg : "+ data + "(-> Duplicate data)");
                   }
              }                 
          }
          catch(Exception e)
          {  
          }
          finally
          {
              System.out.println("\nConnection Terminated.");
              in.close();
              out.close();
              reciever.close();
          }
    }
    
    public static void main(String args[]) throws IOException
     {
        Rcr r = new Rcr();
          r.run();
     }
}
Output:

Sender Window

>javac Sdr.java
>java Sdr
Enter the data to send : asd
ack recerived.
Enter the data to send : qwe
ack recerived.
Enter the data to send : zxc
ack recerived.
Enter the data to send : Terminate
Connection Terminated.
> 

Receiver window

>javac Rcr.java
>java Rcr
Connection established :
Msg received : asd
Msg received : qwe
Msg received : zxc
Connection Terminated.
>           
                           
             
             
             
             

Stop and Wait Protocol With Timeout


// Stop and Wait Protocol
// Using Timers to create delays and Timeout
// Sender

import java.io.*;
import java.net.*;
import java.util.*;

public class Sdr
{
     Socket sender;
   
     ObjectOutputStream out;
     ObjectInputStream in;
   
     String pkt, ack="", data="";
    
     int seq = 0;
    
     boolean Dup = false;
    
     Sdr()
     {
     }
   
     public void run() throws IOException
     {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           
          sender = new Socket("localhost",1500);
          sender.setSoTimeout(5*1000);
          seq = 0;

          out = new ObjectOutputStream(sender.getOutputStream());
          in = new ObjectInputStream(sender.getInputStream());
                       
          while(true)
          {
                  
               try
              {
                   if(Dup)
                   {
                        out.writeObject(pkt);
                        Dup = false;
                   }
                   else
                   {
                        System.out.print("\nEnter the data to send : ");
                        data = br.readLine().trim();
                                               
                        pkt = String.valueOf(seq);
                        pkt = pkt.concat(data);
                        out.writeObject(pkt);
                   }
    
                   out.flush();

                   try
                   {            
                        ack = (String)in.readObject();
                                                                       
                        if(data.equals("Terminate"))
                             break;
                       
                        if(ack.equals(String.valueOf(seq)))
                        {
                             System.out.println("ack received : " + ack);
                             seq = (seq==0)?1:0;
                        }
                        else
                             Dup = true;
                       
                   }  
                   catch(SocketTimeoutException ste)
                   {
                        System.out.println("Ack timeout. resend data");
                        Dup = true;
                   }
              }            
              catch(Exception e)
              {
              }
                  
          }
         
          in.close();
          out.close();
          sender.close();
          System.out.println("\nConnection Terminated.");
         
    }
    
    public static void main(String args[]) throws IOException
     {
        Sdr s = new Sdr();
        s.run();
    }
}            
             



// Stop and Wait Protocol
// Reciever

import java.io.*;
import java.net.*;
import java.util.*;


public class Rcr
{
     ServerSocket reciever;
    
     Socket conc = null;
   
     ObjectOutputStream out;
     ObjectInputStream in;
   
     String ack, pkt, data="";
     int seq = 0, delay ;
  
     Random r = new Random();
     
     Rcr()
     {
     }
   
    
     public void run() throws IOException, InterruptedException
     {
       
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           
          reciever = new ServerSocket(1500,10);
          conc = reciever.accept();
           
          if(conc!=null)
              System.out.println("Connection established :");
                            
          out = new ObjectOutputStream(conc.getOutputStream());
          in = new ObjectInputStream(conc.getInputStream());
             
          while(true)
          {
              try
              {
                   pkt = (String)in.readObject();
                   ack = pkt.substring(0,1);
                   data = pkt.substring(1);
                                     
                   if(data.equals("Terminate"))
                        break;
                       
                   if(Integer.valueOf(ack) == seq)
                   {
                        delay = r.nextInt(10);
                        Thread.currentThread().sleep(delay*1000);
                       
                        System.out.println("\nMsg received : "+data);
                        System.out.println("sending ack " +ack);
                       
                        out.writeObject(ack);
                       
                        seq = (seq==0)?1:0;
                   }
                   else
                        out.writeObject(ack);     
                       
                                 
                   out.flush();
                  
              }                 
         
              catch(Exception e)
              {  
              }
          }

          in.close();
          out.close();
          reciever.close();
          System.out.println("\nConnection Terminated.");
     }
    
    public static void main(String args[]) throws IOException, InterruptedException
     {
          Rcr r = new Rcr();
          r.run();
     }
}