Write a Program to Implement Stop and Wait Selective Repeat Using TCP/UDP
TCP
SERVER
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 Q3_TCPS extends Thread
{
static ServerSocket Serversocket; static DataInputStream dis; static DataOutputStream dos;
public static void main(String[] args) throws SocketException
{
try
{
int a[] = { 11, 22, 33, 44, 55, 66, 77, 88 };
System.out.println("Server is starting."); Serversocket = new ServerSocket(9999);
System.out.println("Server is waiting for client request...");
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);
System.out.println("No of frame is:" + y);
dos.flush();
for (int i = 0; i < a.length; i++)
{
dos.write(a[i]); Thread.sleep(1000);
System.out.println("Send frame is: " + a[i]); dos.flush();
}
int k = dis.read();
Thread.sleep(1000);
System.out.println("\nRetransmit packet no "+(k+1)+"again!!");
dos.write(a[k]); Thread.sleep(2000);
System.out.println("\nSend frame is: " + a[k]); Thread.sleep(3000);
System.out.println("\nAll frame send successfully."); dos.flush();
}
catch (Exception e)
{
System.out.println("Error is:= "+e); System.exit(0);
}
finally
{
try
{
dis.close();
dos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Client
import java.lang.System;
import java.net.*;
import java.io.*;
import java.util.Random;
public class Q3_TCPC extends Thread
{
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");
connection = new Socket(addr, 9999);
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++)
{
Thread.sleep(1000);
System.out.println("Received frame is: " + v[i]);
}
for (int i = 0; i < p; i++)
{
if (v[i] == -1)
{
Thread.sleep(2000); System.out.println("\nRequest to retransmit from packet no "+ (i+1) + " again!!");
n = i;
out.write(n);
out.flush();
}
}
System.out.println(); v[n] = in.read(); Thread.sleep(3000);
System.out.println("Received frame is: " + v[n]); Thread.sleep(3000);
System.out.println("\nAll frame receive successfully.");
}
catch (Exception e)
{
System.out.println("Error is:= "+e); System.exit(0);
}
}
}
OUTPUT
UDP
SERVER
import java.io.*;
import java.net.*;
import java.util.*;
public class Q3_UDPS extends Thread
{
public static void main(String[] args) throws SocketException
{
try
{
int a[] = { 11, 22, 33, 44, 55, 66, 77, 88 };
System.out.println("Server is starting.");
DatagramSocket ds = new DatagramSocket(9999);
InetAddress ia = InetAddress.getLocalHost();
System.out.println("Server is waiting for client request...");
byte b1[] = new byte[100];
DatagramPacket dp = new DatagramPacket(b1,b1.length);
ds.receive(dp);
String str = new String(dp.getData());
str = str.trim(); System.out.println("Client "+str);
System.out.println("The number of packets sent is:" + a.length);
int y = a.length;
byte b2[] = (String.valueOf(y)).getBytes();
DatagramPacket dp1 = new DatagramPacket(b2,b2.length,ia,dp.getPort());
ds.send(dp1);
System.out.println("No of frame is:" + y);
for (int i = 0; i < a.length; i++)
{
byte b_2[] = (String.valueOf(a[i])).getBytes();
DatagramPacket dp2 = new
DatagramPacket(b_2,b_2.length,ia,dp.getPort());
ds.send(dp2); Thread.sleep(1000);
System.out.println("Send frame is: " + a[i]);
}
byte b3[] = new byte[100];
DatagramPacket dp3 = new DatagramPacket(b3,b3.length);
ds.receive(dp3);
String str2 = new String(dp3.getData()); str2 = str2.trim();
int k = Integer.valueOf(str2);
Thread.sleep(1000);
System.out.println("\nRetransmit packet no "+(k+1)+"again!!");
byte b4[] = (String.valueOf(a[k])).getBytes();
DatagramPacket dp4 = new DatagramPacket(b4,b4.length,ia,dp.getPort());
ds.send(dp4);
Thread.sleep(2000);
System.out.println("\nSend frame is: " + a[k]);
Thread.sleep(3000);
System.out.println("\nAll frame send successfully.");
}
catch (Exception e)
{
System.out.println("Error is:= "+e); System.exit(0);
}
}
}
Client
import java.lang.System;
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.Random;
import java.util.*;
public class Q3_UDPC extends Thread
{
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;
DatagramSocket ds = new DatagramSocket(); InetAddress ia = InetAddress.getLocalHost();
byte b1[] = ("Connected.").getBytes();
DatagramPacket dp = new DatagramPacket(b1,b1.length,ia,9999);
ds.send(dp);
byte b2[] = new byte[100];
DatagramPacket dp1 = new DatagramPacket(b2,b2.length); ds.receive(dp1);
String str1 = new String(dp1.getData()); str1 = str1.trim();
int p = Integer.valueOf(str1);
System.out.println("No of frame is:" + p);
for (int i = 0; i < p; i++)
{
byte b_2[] = new byte[100]; DatagramPacket dp2 = new
DatagramPacket(b_2,b_2.length);
ds.receive(dp2);
String str2 = new String(dp2.getData()); str2 = str2.trim();
v[i] = Integer.valueOf(str2);
}
rand = rands.nextInt(p);//FRAME NO. IS RANDOMLY v[rand] = -1;
for (int i = 0; i < p; i++)
{
Thread.sleep(1000); System.out.println("Received frame is: " + v[i]);
}
for (int i = 0; i < p; i++)
{
if (v[i] == -1)
{
Thread.sleep(2000);
System.out.println("\nRequest to retransmit from packet no "+ (i+1) + " again!!");
n = i;
byte b6[] = (String.valueOf(n)).getBytes();
DatagramPacket dp6 = new DatagramPacket(b6,b6.length,ia,9999);
ds.send(dp6);
}
}
System.out.println();
byte b5[] = new byte[100];
DatagramPacket dp5 = new DatagramPacket(b5,b5.length);
ds.receive(dp5);
String str5 = new String(dp5.getData());
str5 = str5.trim();
v[n] = Integer.valueOf(str5);
Thread.sleep(3000);
System.out.println("Received frame is: " + v[n]);
Thread.sleep(3000);
System.out.println("\nAll frame receive successfully.");
}
catch (Exception e)
{
System.out.println("Error is:= "+e); System.exit(0);
}
}
}
OUTPUT
Comment your views on this Article :)
No comments
Comment your views on this article