Showing posts with label stop and wait protocol with timeout. Show all posts
Showing posts with label stop and wait protocol with timeout. Show all posts

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();
     }
}