Skip to content

Instantly share code, notes, and snippets.

@arunsammit
Last active September 24, 2021 08:50
Show Gist options
  • Save arunsammit/3b8c595eaa19e619f03ca3346d70c3fe to your computer and use it in GitHub Desktop.
Save arunsammit/3b8c595eaa19e619f03ca3346d70c3fe to your computer and use it in GitHub Desktop.
simulation of ARQ (stop and wait ) data transfer protocol

ARQ Protocol Simulation in Java

Compile Server using javac Server.java and Client using javac Client.java. Create a messages.txt file which contains the messages that the client will read and send to the server

Then run server using java Server <port number> <probability of error> <channel delay (ms)>

Example: java Server 8080 0.2 500

Run Client using java Client <hostname> <port> <messages filepath>

Example: java Client localhost 8080 ./messages.txt

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Client {
public static void main(String[] args) throws IOException {
if (args.length != 3) {
System.out.println("Usage: java Client <hostname> <port> <filepath>");
System.exit(1);
}
double sendingDelay = 0;
long startTime = System.currentTimeMillis();
try(DatagramSocket socket = new DatagramSocket()){
InetAddress address = InetAddress.getByName(args[0]);
int port = Integer.valueOf(args[1]);
try(BufferedReader in = new BufferedReader(new FileReader(args[2]))){
String value;
while ((value = in.readLine())!=null) {
byte[] buf = value.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
boolean retransmit;
do {
long timeBeforeSend = System.currentTimeMillis();
System.out.println("Trying to send below message in a packet: \n" + value);
socket.send(packet);
// calculating the time required for pushing all bits of datagram into the channel
sendingDelay += System.currentTimeMillis() - timeBeforeSend;
byte[] receiveBuf = new byte[256];
// packet for receiving response from server
DatagramPacket receivePacket = new DatagramPacket(receiveBuf, receiveBuf.length);
// waiting for the response from server
socket.receive(receivePacket);
String received = new String(receivePacket.getData(), 0 , receivePacket.getLength());
// checking the server response
if(received.equals("NACK")){
System.out.println("NACK received: retransmitting");
retransmit = true;
} else if(received.equals("ACK")){
System.out.println("ACK received: successfully transmitted ");
retransmit = false;
} else {
System.err.println("can't identify the received message: " + received);
return;
}
} while (retransmit);
System.out.println("transmission of current packet is complete...");
}
System.out.println("Closing Datagram socket ...");
} catch(FileNotFoundException ex){
System.err.println("Couldn't open the file " + args[2] + ". Please Check the path");
return;
}
long totalDelay = System.currentTimeMillis() - startTime;
double linkUtilization = sendingDelay / totalDelay;
System.out.println("total sending delay is: " + sendingDelay + "ms");
System.out.println("total delay is: " + totalDelay + "ms");
System.out.println("link utilization is: " + linkUtilization);
}
}
}
Sample message 1
message 2
It is the message 3
Message 4 wow!
mess5
it is sixth message
send message 7
message 8
mess 9
it is really a mess 10
Sample message 11
message 12
It is the message 13
Message 14 wow!
mess15
it is 16th message
send message 17th
message 18th
mess 9th
it is really a mess 20
Sample message 21
message 22
It is the message 23
Message 24 wow!
mess25
it is twenty sixth message
send message 27
message 28
mess 29
it is really a mess 30
Sample message 31
message 32
It is the message 33
Message 34 wow!
mess35
it is 36th message
send message 37th
message 38th
mess 39th
it is really a mess 4
import java.net.*;
import java.util.Arrays;
import java.util.List;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
if (args.length != 3) {
System.err.println("Usage: java Server <port number> <probability of error> <channel delay (ms)>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
double probabilityOfError = Double.parseDouble(args[1]);
long channeldelay = Long.parseLong(args[2]);
boolean listening = true;
int messageNumber = 0;
System.out.println("Server started ...");
try (DatagramSocket socket = new DatagramSocket(portNumber)) {
while (listening) {
byte[] buf = new byte[512];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("Waiting for client ...");
// message from client
socket.receive(packet);
// simulating channel delay
Thread.sleep(channeldelay);
// simulating error
double choice = Math.random();
String responseString;
if(choice < probabilityOfError){
System.out.println("Erronous packet received");
responseString = "NACK";
} else {
messageNumber++;
String received = new String(packet.getData(), 0 , packet.getLength());
if(received.equalsIgnoreCase("end")){
listening = false;
}
System.out.println("Received message number " + messageNumber + ":\n" + received );
responseString = "ACK";
}
InetAddress address = packet.getAddress();
byte[] response = responseString.getBytes();
int port = packet.getPort();
packet = new DatagramPacket(response, response.length, address, port);
socket.send(packet);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment