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


 TCP

Client Side:

import java.io.*;

import java.net.*;

public class A5_C_TCP

{

Socket sender;

ObjectOutputStream out;

ObjectInputStream in;

String packet,ack,str, msg;

int n,i=0,sequence=0;

public void run()

{

try

{

BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

System.out.println("Waiting for Connection... ");

sender = new Socket("localhost",2004);

sequence=0;

out=new

ObjectOutputStream(sender.getOutputStream());

out.flush();

in=new ObjectInputStream(sender.getInputStream());

str=(String)in.readObject();

System.out.println("Client "+str);

System.out.print("Enter the data to send:= ");

packet=br.readLine();

System.out.println("\nPacket is:= "+packet);

n=packet.length();

System.out.println();

// System.out.println("N is:= "+n);

do

{

try

{

if(i<n)

{

Substring msg is:= "+msg);

msg=String.valueOf(sequence);

// System.out.println("Before

msg=msg.concat(packet.substring(i,i+1));

// System.out.println("After

Substring msg is:= "+msg);

}

else if(i==n)

{

msg="end";

out.writeObject(msg);

break;

}

"+sequence);

is:= "+ack);

out.writeObject(msg);

sequence=(sequence==0)?1:0;

// System.out.println("sequence is:=

out.flush();

System.out.println("data sent := "+msg);

Thread.sleep(2000);

ack=(String)in.readObject();

// System.out.println("acknowledgment

System.out.println("waiting for

acknowledgment...");

if(ack.equals(String.valueOf(sequence)))

{

recieved\n");

i++;

System.out.println("Packet

}

else

{

Thread.sleep(2000);

resending data...\n");

System.out.println("Time out

sequence=(sequence==0)?1:0;

Thread.sleep(2000);

}

}

catch(Exception e)

{

System.exit(0);

}

}while(i<n+1);

System.out.println("\nAll data sent.");

}

catch(Exception e)

{

System.exit(0);

}

finally

{

try

{

in.close();

out.close();

sender.close();

}

catch(Exception e){}

}

}

public static void main(String args[])

{

A5_C_TCP obj = new A5_C_TCP();

obj.run();

}

}


Server Side:

import java.io.*;

import java.net.*;

public class A5_S_TCP

{

ServerSocket Receiver;

Socket connection=null;

ObjectOutputStream out;

ObjectInputStream in;

String packet,ack,data="";

int i=0,sequence=0;

public void run()

{

try

{

BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

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

Receiver = new ServerSocket(2004,10);

System.out.println("Server is waiting for client

request...");

connection=Receiver.accept();

sequence=0;

System.out.println("Connection established.");

out=new

ObjectOutputStream(connection.getOutputStream());

out.flush();

in=new

ObjectInputStream(connection.getInputStream());

out.writeObject("connected.");

do

{

try

{

"+packet);

packet=(String)in.readObject();

System.out.println("Packet is:=

if(packet.equals("end"))

{

recived is:= "+data);

System.out.println("\nData

String temp = data;

data = temp.replace(data, " ");

}

// System.out.println("sequence is:=

"+sequence);

if(Integer.valueOf(packet.substring(0,1))==sequence)

{

"+data);

is:= "+sequence);

packet is:= "+packet);

data+=packet.substring(1);

System.out.println("data is:=

sequence=(sequence==0)?1:0;

// System.out.println("sequence

System.out.println("\nReceive

Thread.sleep(2000);

}

else

{

System.out.println("\nReceive

packet is:= "+packet+" with duplicate data");

Thread.sleep(2000);

}

if(i<3)

{

System.out.println(String.valueOf(sequence));

out.writeObject(String.valueOf(sequence));i++;

System.out.println("Acknowledgment sended.");

Thread.sleep(2000);

}

else

{

String.valueOf(String.valueOf((sequence+1)%2));

out.writeObject(String.valueOf((sequence+1)%2));

i=0;

System.out.println("Acknowledgment sended.");

Thread.sleep(2000);

}

}

catch(Exception e)

{

System.exit(0);

}

}while(!packet.equals("end"));

out.writeObject("\nconnection ended.");

}

catch(Exception e)

{

}

finally

{

try

{

in.close();

out.close();

Receiver.close();

}

catch(Exception e)

{

System.exit(0);

}

}

}

public static void main(String args[])

{

A5_S_TCP obj = new A5_S_TCP();

obj.run();

}

}

Output:


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

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


UDP

Client Side:

import java.io.*;

import java.net.*;

import java.util.*;

public class A5_C_UDP extends Thread

{

String packet,ack,str, msg;

int n,i=0,sequence=0;

public void run()

{

try

{

Scanner scan = new Scanner(System.in);

DatagramSocket ds = new DatagramSocket();

InetAddress ia = InetAddress.getLocalHost();

sequence=0;

byte b1[] = ("Connected.").getBytes();

DatagramPacket dp = new

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

ds.send(dp);

System.out.println("Server Connected.");

System.out.print("Enter the data to send:= ");

packet=scan.nextLine();

System.out.println("\nPacket is:= "+packet);

packet = packet.replace(" ","");

n=packet.length();

System.out.println();

System.out.println("N is:= "+n);

do

{

try

{

if(i<n)

{

msg=String.valueOf(sequence);

// System.out.println("Before

Substring msg is:= "+msg);

msg=msg.concat(packet.substring(i,i+1));

// System.out.println("After

Substring msg is:= "+msg);

}

else if(i==n)

{

(String.valueOf(msg)).getBytes();

msg="end";

byte b4[] =

DatagramPacket dp4 = new

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

ds.send(dp4);

break;

}

(String.valueOf(msg)).getBytes();

byte b3[] =

DatagramPacket dp3 = new

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

ds.send(dp3);

sequence=(sequence==0)?1:0;

// System.out.println("sequence is:=

"+sequence);

System.out.println("data sent := "+msg);

Thread.sleep(2000);

DatagramPacket(b5,b5.length);

is:= "+ack);

byte b5[] = new byte[100];

DatagramPacket dp5 = new

ds.receive(dp5);

String str5 = new String(dp5.getData());

ack = str5.trim();

// System.out.println("acknowledgment

System.out.println("waiting for

acknowledgment...");

if(ack.equals(String.valueOf(sequence)))

{

recieved\n");

i++;

System.out.println("Packet

Thread.sleep(2000);

}

resending data...\n");

else

{

}

System.out.println("Time out

sequence=(sequence==0)?1:0;

Thread.sleep(2000);

}

catch(Exception e)

{

System.exit(0);

}

}while(i<n+1);

System.out.println("\nAll data sent.");

}

catch(Exception e)

{

System.exit(0);

}

}

public static void main(String args[])

{

A5_C_UDP obj = new A5_C_UDP();

obj.start();

}

}


Server Side:

import java.io.*;

import java.net.*;

public class A5_S_UDP extends Thread

{

ServerSocket Receiver;

Socket connection=null;

ObjectOutputStream out;

ObjectInputStream in;

String packet,ack,data="";

int i=0,sequence=0;

public void run()

{

try

{

request...");

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

DatagramSocket ds = new DatagramSocket(9999);

InetAddress ia = InetAddress.getLocalHost();

System.out.println("Server is waiting for client

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);

sequence=0;

do

{

try

{

DatagramPacket(b2,b2.length);

"+packet);

byte b2[] = new byte[100];

DatagramPacket dp2 = new

ds.receive(dp2);

String str2 = new String(dp2.getData());

packet = str2.trim();

System.out.println("Packet is:=

if(packet.equals("end"))

{

recived is:= "+data);

System.out.println("\nData

String temp = data;

data = temp.replace(data, " ");

}

// System.out.println("sequence is:=

"+sequence);

if(Integer.valueOf(packet.substring(0,1))==sequence)

{

"+data);

data+=packet.substring(1);

System.out.println("data is:=

sequence=(sequence==0)?1:0;

is:= "+sequence);

packet is:= "+packet);

// System.out.println("sequence

System.out.println("\nReceive

Thread.sleep(2000);

}

else

{

System.out.println("\nReceive

packet is:= "+packet+" with duplicate data");

Thread.sleep(2000);

}

if(i<3)

{

(String.valueOf(sequence)).getBytes();

byte b3[] =

DatagramPacket dp3 = new

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

ds.send(dp3);

i++;

System.out.println("Acknowledgment sended.");

Thread.sleep(2000);

}

else

{

byte b4[] =

(String.valueOf((sequence+1)%2)).getBytes();

DatagramPacket dp4 = new

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

ds.send(dp4);

i=0;

System.out.println("Acknowledgment sended.");

Thread.sleep(2000);

}

}

catch (Exception e)

{

System.exit(0);

}

}while(!packet.equals("end"));

byte b5[] = ("connection ended.").getBytes();

DatagramPacket dp5 = new

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

ds.send(dp5);

}

catch (Exception e)

{

System.exit(0);

}

}

public static void main(String args[])

{

A5_S_UDP obj = new A5_S_UDP();

obj.start();

}

}

Output:


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

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


Stop and wait protocol with both TCP and UDP

Food habits and monsoon

Comment your views on this Article :)


Thank you for visiting my blog :)

No comments

Comment your views on this article

Powered by Blogger.