Selective Repeat Protocol

We have already learned about Go Back N, one of the sliding window protocols. If you are not aware, here you go. Now, we shall look into one more implementation of sliding window protocol, i.e. Selective Repeat.

Selective Repeat:

The method used in Go Back N is cumulative acknowledgment to acknowledge the packets. Now, selective repeat (SR) uses independent acknowledgment. It means that each frame that is sent to the receiver will be acknowledged independently. For more reference on types of acknowledgments, click here. Let’s see how SR actually works.

Fig 1. Selective Repeat.

Suppose, there are five frames that are to be transmitted. The window size is two. The frames 0 and 1 are transmitted initially. The acknowledgment for frame 0 is received by the sender and the window is slid to the next frames and 2 is also transmitted. But the acknowledgment for frame 1 is not received by the sender. The loss might be from the sender or the receiver. But the sender instead of sending the whole window, it searches for the missing packet and sends only that particular frame, in this case, frame 1. With reference to Fig 1.

If there is a corrupted frame from the frames that are sent by the sender, then the packets are not simply discarded. The receiver will send a Negative Acknowledgment (NACK) corresponding to the corrupted frame asking for a retransmission. The same procedure continues to send the corrupted frame again, i.e searching.

The receiver doesn’t discard the next frame i.e. 2 as in Go Back N. It stores the frame in its window. This is why the frames are not received by the receiver in an orderly fashion in the selective repeat protocol. The arrangement of frames is similar to the operation of the Linked List. The new frames that are received will be appended to the list and the sorting has to be performed at the receiver’s side. Thus two operations are involved in SR protocol. They are searching at the sender’s end to transmit the lost frame and sorting to sort the frames at the receiver’s end.

Key takeaways:

  1. The size of the window of the sender is equal to that of the receiver.
  2. The size of the window should be less than or equal to half of the sequence numbers. If this condition is not satisfied, the following consequence may occur. Suppose the acknowledgment is lost. The sender might send the new packets and the receiver will be in the perspective that they are the retransmitted frames.
  3. Independent acknowledgment is used in the SR protocol.
  4. The corrupted frames are not discarded instead a negative acknowledgment is sent. (if NACK is enabled)
  5. SR protocol accepts the out of order frames.
  6. SR protocol requires sorting at the receiver side and searching at the sender’s side.

Implementation of Selective Repeat in Java:

Client Program: File: SRc.java

import java.lang.System;
import java.net.*; 
import java.io.*;
import java.util.Random;

public class SRc {
	static Socket connection;

	public static void main(String a[]) throws SocketException {
		try {
			int v[] = new int[10]; 
			int n = 0;
			Random rands = new Random();
			int rand = 0; 
 			 
			InetAddress addr = InetAddress.getByName("Localhost");
			System.out.println(addr);
			connection = new Socket(addr, 8011);
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			DataInputStream in = new DataInputStream(
					connection.getInputStream());
			int p = in.read();
			System.out.println("No of frame is:" + p);

			for (int i = 0; i < p; i++) {
				v[i] = in.read();
				System.out.println(v[i]);
			}
			rand = rands.nextInt(p);//FRAME NO. IS RANDOMLY GENERATED			
			v[rand] = -1;
			for (int i = 0; i < p; i++){
					System.out.println("Received frame is: " + v[i]);

			}
			for (int i = 0; i < p; i++)
				if (v[i] == -1) {
					System.out.println("Request to retransmit from packet no "
							+ (i+1) + " again!!");
					n = i;
					out.write(n);
					out.flush();
				}
			System.out.println();
				v[n] = in.read();
				System.out.println("Received frame is: " + v[n]);
			System.out.println("quiting");
        } 
        catch (Exception e) {
			System.out.println(e);
		}

	}
}
/*source:
https://www.way2techin.com/2018/01/selective-repeat-sliding-window-protocol.html
*/

Server side: File Name: SRs.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class SRs {
	static ServerSocket Serversocket;
	static DataInputStream dis;
	static DataOutputStream dos;

	public static void main(String[] args) throws SocketException {
		try {
			int a[] = { 30, 40, 50, 60, 70, 80, 90, 100 };
			Serversocket = new ServerSocket(8011);
			System.out.println("waiting for connection");
			Socket client = Serversocket.accept();
			dis = new DataInputStream(client.getInputStream());
			dos = new DataOutputStream(client.getOutputStream());
			System.out.println("The number of packets sent is:" + a.length);
			int y = a.length;
			dos.write(y);
			dos.flush();
			for (int i = 0; i < a.length; i++) {
				dos.write(a[i]);
				dos.flush();
			}
			int k = dis.read();
			dos.write(a[k]);
			dos.flush();
		}
		catch (IOException e){
			System.out.println(e);
		}
		finally{
			try{
				dis.close();
				dos.close();
			}
			catch (IOException e){
				e.printStackTrace();
			}
		}
	}
}
/* source:
https://www.way2techin.com/2018/01/selective-repeat-sliding-window-protocol.html
*/

Quick Tip: Execute the two programs in separate IDEs. Those can be Command prompt and any Java IDE. If your IDE supports parallel execution of two programs, you are free to use.

Difference between Go Back N and Selective Repeat:

Go Back N ProtocolSelective Repeat Protocol
If one of the frames is lost, then all the frames of the window are retransmitted.The only lost frame is retransmitted.
The receiver window size is always equal to 1.The receiver window size is always equal to the window size of the sender.
It is less complex.It is more complex.
Out of order frames are not accepted.Out of order frames are accepted.
All the frames are retransmitted when one of the frames is corrupted.The only frame that is corrupted is retransmitted.
It doesn’t require sorting and searching.It requires searching and sorting.
It has a high error rate and wastes a lot of bandwidth.There is a low loss of bandwidth.
Table. Go Back N and Selective Repeat.

References and Recommendations:

2 thoughts on “Selective Repeat Protocol

Leave a comment