Stop and Wait Protocol in Java | StarkeeCode

Image1: Stop and Wait Protocol in Java java.starkeecode.online

Using Java programming, Stop and wait protocol or ARQ is implemented.
This method is used to transfer data(any information) between two programs in the same packages with the help of Sockets having a unique id to identify.

Detail:
  • The frame is sent one-by-one to the Receiver(Server) from the Sender(Client) side.
  • Acknowledgment is sent from Server to the Client
  • The next frame is sent after receiving the acknowledgment from the server-side.
This technique/method is known to be 
STOP-and-WAIT Protocol.

 is similar to the Stop and Wait protocol but it is more efficient and sends multiple frames at a time. 

Stop-and-wait ARQ: (source: wiki)

  1. Stop and Wait Protocol also referred to as alternating bit protocol, is a method in telecommunications to send information between two connected devices. 
  2. It ensures that information is not lost due to dropped packets and that packets are received in the correct order. It is the simplest automatic repeat-request (ARQ) mechanism.
  3.  A stop-and-wait ARQ sender sends one frame at a time; it is a special case of the general sliding window protocol with transmitting and receiving window sizes equal to one and greater than one respectively. 
  4. After sending each frame, the sender doesn't send any further frames until it receives an acknowledgment (ACK) signal. 
  5. After receiving a valid frame, the receiver sends an ACK. If the ACK does not reach the sender before a certain time, known as the timeout, the sender sends the same frame again. 
  6. The timeout countdown is reset after each frame transmission.
  7. The above behavior is a basic example of Stop-and-Wait. 
  8. However, real-life implementations vary to address certain issues of design.

Program Coding:

RECEIVER(SERVER)

import java.io.*;

import java.net.*;

import java.util.*;

/** * * @author StarkeeCode */

public class receiver {

    public static void main(String args[])

{

    String h="Serverhost";

    int q=5000;

    int i;

        try

        {         

        ServerSocket ss2;

            ss2 = new ServerSocket(8000);

            Socket s1 =ss2.accept();

        DataInputStream dd1= new DataInputStream(s1.getInputStream());

        Integer i1 =dd1.read();

        for(i=0;i<i1;i++)

        {

            ServerSocket ss1;

            ss1 = new ServerSocket(9000+i);

            Socket s =ss1.accept();

        DataInputStream dd= new DataInputStream(s.getInputStream());

        String sss1 = dd.readUTF();

            System.out.println(sss1);

            System.out.println("Frame "+ i+" received");

        DataOutputStream d1 = new DataOutputStream(s.getOutputStream());

        d1.write(i);

         System.out.println("ACK sent for "+ i); 

        }

        }

        catch(Exception ex)

        {

         System.out.println("Error"+ex);

                }

}

}

SENDER(CLIENT)

import java.io.*;

import java.net.*;

import java.util.Scanner;

/** * * @author StarkeeCode */

public class sender {

    public static void main(String args[])

{

   int p=9000,i,q=8000;

    String h="localhost";

    try

    {

        Scanner scanner = new Scanner(System.in);

    System.out.print("Enter number of frames : ");

    int number = scanner.nextInt();

    if(number==0)

    {

        System.out.println("No frame is sent");

    }

    else

            {           

          Socket s2;

        s2= new Socket(h,q);

        DataOutputStream d1 = new DataOutputStream(s2.getOutputStream());

        d1.write(number);

          }

    String str1;

        for (i=0;i<number;i++)

        {                 

    System.out.print("Enter message : ");

    String name = scanner.next();

    System.out.println("Frame " + i+" is sent"); 

    Socket s1;

        s1= new Socket(h,p+i);

        DataOutputStream d = new DataOutputStream(s1.getOutputStream());

        d.writeUTF(name);

        DataInputStream dd= new DataInputStream(s1.getInputStream());

        Integer sss1 = dd.read();

        System.out.println("Ack for :" + sss1 + " is  received");

        }

    }

    catch(Exception ex)

            {

                System.out.println("ERROR :"+ex);

            }

}

Output:

stop and wait client side output
client class

stop and wait server side output
server class

Reactions

Post a Comment

3 Comments

  1. Can you please explain what is q in sender and receiver and why their values are 8000 and 5000?

    ReplyDelete
    Replies
    1. q and p are just variables to hold the portal id (5000,8000) for sending the data between the protocols

      Delete
  2. 1xbet korean | Is 1xbet korean a good option? | Legalbet.co.kr
    1xbet korean. 1xbet korean. 1xbet korean. 1xbet korean. 1xbet korean. 1xbet korean. 1xbet korean. 1xbet 1xbet korean korean. 1xbet korean. 카지노사이트 1xbet 메리트 카지노 주소 korean.

    ReplyDelete