Write a program to implement Stop and Wait Protocol using both TCP and UDP


Client Side:

TCP

import java.io.*;

import java.util.*;

import java.net.*;

class A2_C_TCP

{

public static void main(String args[]) throws Exception

{

A2_C_TCP obj = new A2_C_TCP();

obj.run();

}

public void run() throws Exception

{

Scanner scan = new Scanner(System.in);

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

int n=scan.nextInt();

Socket s = new Socket("localhost", 9999);

PrintStream ps = new PrintStream(s.getOutputStream());

for(int i=0;i<=n;)

{

if(i == n)

{

ps.println("Exit");

break;

}

System.out.println("\nFrame "+(i+1)+" sent."); ps.println(i);

BufferedReader br = new BufferedReader(new

InputStreamReader (s.getInputStream()));

String ack = br.readLine();

if(ack != null)

{

from server.");

}

}

else

{

}

System.out.println("Acknowledgement was received

i++;

Thread.sleep(2000);

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

}

}


Server Side:

import java.io.*;

import java.util.*;

import java.net.*;

class A2_S_TCP

{

public static void main(String args[])throws Exception

{

A2_S_TCP obj = new A2_S_TCP();

obj.run();

}

public void run()throws Exception

{

String temp="any message", str="Exit";

System.out.println("Server is starting.");

ServerSocket ss = new ServerSocket(9999);

System.out.println("Server is waiting for client request...");

Socket s = ss.accept();

BufferedReader br = new BufferedReader(new

InputStreamReader (s.getInputStream()));

PrintStream ps = new PrintStream(s.getOutputStream());

while(temp.compareTo(str) != 0)

{

Thread.sleep(1000);

temp=br.readLine();

if(temp.compareTo(str)==0)

{

break;

}

System.out.println("\nReceived Frame

"+((Integer.valueOf(temp)+1)));

Thread.sleep(500);

ps.println("Received");

}

System.out.println("\nAll frame was received successfully.");

}

}

Output:


Write a program to implement Stop and Wait Protocol using both TCP and UDP

Write a program to implement Stop and Wait Protocol using both TCP and UDP


UDP

Client Side:

import java.io.*;

import java.util.*;

import java.net.*;

class A2_C_UDP extends Thread

{

public static void main(String args[]) throws Exception

{

A2_C_UDP obj = new A2_C_UDP();

obj.start();

}

public void run()

{

try

{

Scanner scan = new Scanner(System.in);

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

int n=scan.nextInt();

DatagramSocket ds = new DatagramSocket();

InetAddress ia4 = InetAddress.getLocalHost();

byte b4[] = String.valueOf(n).getBytes();

DatagramPacket dp4 = new

DatagramPacket(b4,b4.length,ia4,9999);

ds.send(dp4);

for(int i=0;i<=n;)

{

if(i == n)

{

InetAddress ia = InetAddress.getLocalHost();

byte b1[] = String.valueOf("Exit").getBytes();

DatagramPacket dp = new

DatagramPacket(b1,b1.length,ia,9999);

ds.send(dp);

break;

}

System.out.println("\nFrame "+(i+1)+" sent.");

InetAddress ia = InetAddress.getLocalHost();

byte b2[] = String.valueOf((i)).getBytes();

DatagramPacket dp2 = new

DatagramPacket(b2,b2.length,ia,9999);

ds.send(dp2);

acknowledgement...");

System.out.println("Waiting for

Thread.sleep(2000);

byte b3[] = new byte[50];

DatagramPacket dp3 = new

DatagramPacket(b3,b3.length);

ds.receive(dp3);

String ack = new String(dp3.getData());

ack=ack.trim();

int a = Integer.valueOf(ack);

if(a == i)

{

System.out.println("Acknowledgement was

received for frame "+(i+1)+" as "+(a+1));

i++;

}

else

{

}

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

Thread.sleep(2000);

}

}

catch(Exception e1)

{

System.out.println("Error is:= "+e1);

System.exit(0);

}

}

}


Server side:

import java.io.*;

import java.util.*;

import java.net.*;

class A2_S_UDP extends Thread

{

public static void main(String args[]) throws Exception

{

A2_S_UDP obj = new A2_S_UDP();

obj.start();

}

public void run()

{

try

{

String temp="", str="Exit";

System.out.println("Server is starting.");

DatagramSocket ds = new DatagramSocket(9999);

DatagramSocket ds2 = new DatagramSocket(9998);

System.out.println("Server is waiting for client request...");

byte b3[] = new byte[50];

DatagramPacket dp3 = new DatagramPacket(b3,b3.length);

ds.receive(dp3);

String frameLength = new String(dp3.getData());

frameLength = frameLength.trim();

int n = Integer.valueOf(frameLength);

System.out.println("Frame Length is:= "+frameLength);

while(temp.compareTo(str) != 0)

{

Thread.sleep(2000);

byte b1[] = new byte[50];

DatagramPacket dp = new

DatagramPacket(b1,b1.length);

ds.receive(dp);

temp = new String(dp.getData());

temp = temp.trim();

if(temp.compareTo(str)==0)

{

break;

}

System.out.println("\nReceived Frame

"+((Integer.valueOf(temp)+1)));

System.out.println("Sending acknowledgement...");

Thread.sleep(2000);

InetAddress ia = InetAddress.getLocalHost();

int t = Integer.valueOf(temp);

byte b2[] = String.valueOf(t).getBytes();

DatagramPacket dp2 = new

DatagramPacket(b2,b2.length,ia,dp.getPort());

ds2.send(dp2);

int f = Integer.valueOf(temp);

System.out.println("Acknowledgement was sended

for frame "+(f+1)+" as "+(t+1));

}

System.out.println("\nAll frame was received

successfully.");

}

catch(Exception e1)

{

System.out.println("Error is:= "+e1);

System.exit(0);

}

}

}

Output:


Write a program to implement Stop and Wait Protocol using both TCP and UDP

Write a program to implement Stop and Wait Protocol using both TCP and UDP

Know more about latest IELTS experience 2021


Comment your views on this Article :)

Thank you for visiting my blog :)

No comments

Comment your views on this article

Powered by Blogger.