Header Ads

Selective Repeat Sliding Window Protocol

Selective Repeat :


With the use of multiple frames for a single message, the stop-and-wait protocol does not perform well. Only one frame at a time can be in transit. Efficiency can be greatly improved by allowing multiple frames to be in transit at the same time. Efficiency can also be improved by making use of the full-duplex line. To keep track of the frames, sender station sends sequentially numbered frames. Since the sequence number to be used occupies a field in the frame, it should be of limited size. If the header of the frame allows k bits, the sequence numbers range from 0 to 2k – 1. Sender maintains a list of sequence numbers that it is allowed to send (sender window).



Program :


Client Side :
//................CLIENT SIDE (SELECTIVE REPEAT).............//

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

public class cli {
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]);
//g[i] = 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);
}

}
}

/* OUTPUT

[root@localhost sinhgad]# java cli
Localhost/127.0.0.1
No of frame is:8
30
40
50
60
70
80
90
100
Received frame is: 30
Received frame is: 40
Received frame is: 50
Received frame is: -1
Received frame is: 70
Received frame is: 80
Received frame is: 90
Received frame is: 100
Request to retransmit from packet no 4 again!!

Received frame is: 60
quiting
*/


Server Side:
//.....................SERVER SIDE (SELECTIVE REPEAT)..........//

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

}
}
}

/* OUTPUT

[sinhgad@localhost ~]$ su
Password:
[root@localhost sinhgad]# javac ser.java
[root@localhost sinhgad]# java ser
waiting for connection
The number of packets sent is:8
[root@localhost sinhgad]#

*/

No comments:

Powered by Blogger.