1.
Develop a program for error detecting code using CRC-CCITT
(16- bits).
import [Link];
import [Link].*;
public class CRC1
public static void main(String args[])
Scanner sc = new Scanner([Link]);
//Input Data Stream
[Link]("Enter message bits: ");
String message = [Link]();
[Link]("Enter generator: ");
String generator = [Link]();
int data[] = new int[[Link]() + [Link]() - 1];
int divisor[] = new int[[Link]()];
for(int i=0;i<[Link]();i++)
data[i] = [Link]([Link](i)+"");
for(int i=0;i<[Link]();i++)
divisor[i] = [Link]([Link](i)+"");
//Calculation of CRC
for(int i=0;i<[Link]();i++)
if(data[i]==1) for(int j=0;j<[Link];j++)
data[i+j] ^= divisor[j];
//Display CRC
[Link]("The checksum code is: ");
for(int i=0;i<[Link]();i++) data[i] =
[Link]([Link](i)+"");
for(int i=0;i<[Link];i++)
[Link](data[i]);
[Link]();
//Check for input CRC code
[Link]("Enter checksum code: ");
message = [Link]();
[Link]("Enter generator: ");
generator = [Link](); data = new int[[Link]() +
[Link]() - 1];
divisor = new int[[Link]()];
for(int i=0;i<[Link]();i++)
data[i] = [Link]([Link](i)+"");
for(int i=0;i<[Link]();i++)
divisor[i] = [Link]([Link](i)+"");
//Calculation of remainder
for(int i=0;i<[Link]();i++)
if(data[i]==1)
for(int j=0;j<[Link];j++)
data[i+j] ^= divisor[j];
//Display validity of data
boolean valid = true;
for(int i=0;i<[Link];i++)
if(data[i]==1)
valid = false; break;
if(valid==true)
[Link]("Data stream is valid");
else
[Link]("Data stream is invalid. CRC error occurred.");
2. Develop a program for congestion control using leaky bucket
algorithm.
import [Link];
import [Link].*;
public class leaky {
public static void main(String[] args) {
int i; // Declaring a variable 'i' for loop control.
int a[] = new int[20];
int buck_rem = 0;
int buck_cap = 4;
int rate = 3;
int sent, recv;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of packets"); int n = [Link]();
[Link]("Enter the packets");
for (i = 1; i <= n; i++) a[i] = [Link]();
[Link]("Clock \t packet size \t accept \t sent \t remaining");
for (i = 1; i <= n; i++) {
if (a[i] != 0) {
if (buck_rem + a[i] > buck_cap) recv = -1
else { recv = a[i];
buck_rem += a[i];
}}
else { recv = 0;
if (buck_rem != 0)
if (buck_rem < rate) {
sent = buck_rem;
buck_rem = 0;
} else {
sent = rate; buck_rem = buck_rem - rate;
}}
else { sent = 0}
(recv == -1)
if (recv == -1)
[Link](i + "\t\t" + a[i] + "\t dropped \t" + sent + "\t" +
buck_rem); else // Otherwise, print the accepted, sent, and remaining
values. [Link](i + "\t\t" + a[i] + "\t\t" + recv + "\t" + sent + "\
t" + buck_rem); } } }
3. Develop a program for simple RSA algorithm to encrypt and
decrypt the data.
import [Link];
import [Link];
import [Link];
import [Link];
public class RSA {
private BigInteger p,q,N,phi,e,d;
private int bitlength=1024;
private Random r;
public RSA()
r=new Random();
p=[Link](bitlength,r);
q=[Link](bitlength,r);
[Link]("Prime number p is"+p);
[Link]("prime number q is"+q);
N=[Link](q);
phi=[Link]([Link]).multiply([Link]([Link]));
e=[Link](bitlength/2,r);
while([Link](e).compareTo([Link])>0&&[Link](phi)<0)
[Link]([Link]);
[Link]("Public key is"+e); d=[Link](phi);
[Link]("Private key is"+d);
public RSA(BigInteger e,BigInteger d,BigInteger N)
this.e=e;
this.d=d;
this.N=N;
public static void main(String[] args)throws IOException
RSA rsa=new RSA();
DataInputStream in=new DataInputStream([Link]);
String testString; [Link]("Enter the plain text:");
testString=[Link]();
[Link]("Encrypting string:"+testString);
[Link]("string in
bytes:"+bytesToString([Link]()));
byte[] encrypted=[Link]([Link]());
byte[] decrypted=[Link](encrypted);
[Link]("Dcrypting Bytes:"+bytesToString(decrypted));
[Link]("Dcrypted string:"+new String(decrypted));
private static String bytesToString(byte[] encrypted)
String test=" "; for(byte b:encrypted)
test+=[Link](b);
return test;
public byte[]encrypt(byte[]message)
return(new BigInteger(message)).modPow(e,N).toByteArray();
public byte[]decrypt(byte[]message)
return(new BigInteger(message)).modPow(d,N).toByteArray();
[Link] TCP/IP sockets, write a client – server program to make
the client send the file name and to make the server send back
the contents of the requested file if present.
// TCP Server :
import [Link].*;
import [Link].*;
public class TCPS
public static void main(String[] args) throws Exception
ServerSocket sersock=new ServerSocket(4000);
[Link]("Server ready for connection");
Socket sock=[Link]();
[Link]("Connection Is successful and waiting for chatting");
InputStream istream=[Link]();
BufferedReader fileRead=new BufferedReader(new
InputStreamReader(istream));
String fname=[Link]();
BufferedReader ContentRead=new BufferedReader(new
FileReader(fname));
OutputStream ostream=[Link]();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=[Link]())!=null)
[Link](str);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
TCP Client:
import [Link].*;
import [Link].*;
public class TCPC
public static void main(String[] args) throws Exception
Socket sock=new Socket("127.0.01",4000);
[Link]("Enter the filename");
BufferedReader keyRead=new BufferedReader(new
InputStreamReader([Link]));
String fname=[Link]();
OutputStream ostream=[Link]();
PrintWriter pwrite=new PrintWriter(ostream,true);
[Link](fname);
InputStream istream=[Link]();
BufferedReader socketRead=new BufferedReader(new
InputStreamReader(istream));
String str;
while((str=[Link]())!=null)
[Link](str);
[Link]();
[Link]();
[Link]();
}
5. Develop a program on datagram socket for client/server to
display the messages on client side, typed at the server side.
//UDP Sever:
import [Link].*;
import [Link];
class UDPServer
public static void main(String args[])throws Exception
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData=new byte[1024];
byte[] sendData=new byte[1024];
while(true)
[Link]("Server is Up");
DatagramPacket receivePacket=new
DatagramPacket(receiveData,[Link]);
[Link](receivePacket);
String sentence=new String([Link]());
[Link]("RECEIVED:"+sentence);
InetAddress IPAddress=[Link]();
int port=[Link]();
String capitalizedSentence=[Link]();
sendData=[Link]();
DatagramPacket sendPacket=new
DatagramPacket(sendData,[Link],IPAddress,port);
[Link](sendPacket);
}
//UDP Client :
import [Link].*;
import [Link].*;
import [Link];
class UDPClient
public static void main(String[] args)throws Exception
BufferedReader inFromUser=new BufferedReader(new
InputStreamReader([Link])); DatagramSocket clientSocket=new
DatagramSocket();
InetAddress IPAddress=[Link]("localhost");
byte[] sendData=new byte[1024];
byte[] receiveData=new byte[1024];
[Link]("Enter the sting to be converted in to Upper case");
String sentence=[Link](); sendData=[Link]();
DatagramPacket sendPacket=new
DatagramPacket(sendData,[Link],IPAddress,9876);
[Link](sendPacket);
DatagramPacket receivePacket=new
DatagramPacket(receiveData,[Link]);
[Link](receivePacket);
String modifiedSentence=new String([Link]());
[Link]("FROM SERVER:"+modifiedSentence);
[Link]();
6. Develop a program to find the shortest path between vertices
using the Bellman-Ford and path vector routing algorithm.
import [Link];
class Graph {
class Edge {
int source, destination, weight;
Edge(int source, int destination, int weight) {
[Link] = source; [Link] = destination;
[Link] = weight; } }
int vertices, edges;
Edge[] edgeList;
Graph(int vertices, int edges) {
[Link] = vertices;
[Link] = edges;
edgeList = new Edge[edges];
void addEdge(int edgeIndex, int source, int destination, int weight)
{ edgeList[edgeIndex] = new Edge(source, destination, weight);
void bellmanFord(int startVertex) {
int[] distances = new int[vertices];
[Link](distances, Integer.MAX_VALUE);
distances[startVertex] = 0;
for (int i = 1; i < vertices; i++) {
for (int j = 0; j < edges; j++) {
int u = edgeList[j].source; int v = edgeList[j].destination;
int weight = edgeList[j].weight;
if (distances[u] != Integer.MAX_VALUE && distances[u] + weight <
distances[v]) { distances[v] = distances[u] + weight; } } } // Check for
negative-weight cycles for (int j = 0; j < edges; j++) {
int u = edgeList[j].source; int v = edgeList[j].destination;
int weight = edgeList[j].weight;
if (distances[u] != Integer.MAX_VALUE && distances[u] + weight <
distances[v]) { [Link]("Graph contains a negative-weight
cycle"); return; } } printSolution(distances, startVertex); }
void printSolution(int[] distances, int startVertex) {
[Link]("Vertex distances from source vertex " + startVertex +
":");
for (int i = 0; i < vertices; i++) {
[Link]("To Vertex " + i + " is " + distances[i]); } }
void distanceVectorRouting(int[][] graph, int startVertex) {
int[] distances = new int[vertices];
[Link](distances, Integer.MAX_VALUE);
distances[startVertex] = 0; boolean updated;
do { updated = false;
for (int u = 0; u < vertices; u++) {
for (int v = 0; v < vertices; v++) {
if (graph[u][v] != Integer.MAX_VALUE && distances[u] !=
Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
distances[v] = distances[u] + graph[u][v]; updated = true; } } } }
while (updated); printSolution(distances, startVertex); }
public static void main(String[] args) {
int vertices = 5;
int edges = 8;
Graph graph = new Graph(vertices, edges); // Adding edges to the graph
[Link](0, 0, 1, -1);
[Link](1, 0, 2, 4);
[Link](2, 1, 2, 3);
[Link](3, 1, 3, 2);
[Link](4, 1, 4, 2);
[Link](5, 3, 2, 5);
[Link](6, 3, 1, 1);
[Link](7, 4, 3, -3); // Running Bellman-Ford Algorithm
[Link]("Bellman-Ford Algorithm:"); [Link](0); //
Distance Vector Routing [Link]("\nDistance Vector Routing
Algorithm:");
int[][] routingGraph = { {0, -1, 4, Integer.MAX_VALUE,
Integer.MAX_VALUE}, {Integer.MAX_VALUE, 0, 3, 2, 2},
{Integer.MAX_VALUE, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
Integer.MAX_VALUE}, {Integer.MAX_VALUE, 1, 5, 0, Integer.MAX_VALUE},
{Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, -3, 0}, };
[Link](routingGraph, 0); } }
7. Develop a program to implement a sliding window protocol in
the data link layer.
import [Link];
import [Link];
class GoBackNProtocol {
private final int WINDOW_SIZE; // Window size
private final int TOTAL_FRAMES; // Total number of frames
private int nextFrameToSend = 0; // Index of the next frame to send
private int frameExpectedByReceiver = 0; // The frame that the
receiver expects
public GoBackNProtocol(int totalFrames, int windowSize) {
this.TOTAL_FRAMES = totalFrames;
this.WINDOW_SIZE = windowSize;
public void sendFrames() {
Random random = new Random();
while (nextFrameToSend < TOTAL_FRAMES) {
// Simulate sending frames in the current window
for (int i = 0; i < WINDOW_SIZE && nextFrameToSend <
TOTAL_FRAMES; i++) {
[Link]("Sender: Sending frame " +
nextFrameToSend);
nextFrameToSend++;
// Simulate receiving an ACK with some random loss
if ([Link]()) {
frameExpectedByReceiver += WINDOW_SIZE;
[Link]("Receiver: ACK received for frames up to " +
frameExpectedByReceiver);
} else {
[Link]("Receiver: ACK lost, resending frames from "
+ (frameExpectedByReceiver));
nextFrameToSend = frameExpectedByReceiver; // Go back to
the unacknowledged frame
[Link]("All frames sent and acknowledged
successfully.");
public static void main(String[] args) {
int totalFrames = 10; // Total number of frames to send
int windowSize = 4; // Window size
GoBackNProtocol protocol = new GoBackNProtocol(totalFrames,
windowSize);
[Link]();
}}