0% found this document useful (0 votes)
3 views2 pages

Java Networking and Algorithms Examples

Uploaded by

ojusanjan375
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Networking and Algorithms Examples

Uploaded by

ojusanjan375
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program – 01 Program – 02

Leaky Bucket Bellman-Ford algorithm

import [Link]; import [Link];


public class LeakyBucket { public class Ford
public static void main(String[] args) { {
Scanner in = new Scanner([Link]); private int[] distance;
int buck_cap = 4, rate = 3; private int numVertices;
int buck_rem = 0; public static final int MAX_VALUE = 999;
int[] a = new int[20]; public Ford(int numVertices) {
[Link]("Enter number of packets:"); [Link] = numVertices;
int n = [Link](); distance = new int[numVertices + 1];
[Link]("Enter the packets (packet sizes):"); }
for (int i = 0; i < n; i++) { public void bellmanFordEvaluation(int source, int[][] adjacencyMatrix)
a[i] = [Link](); {
} for (int node = 1; node <= numVertices; node++)
[Link]("Clock\tPacketSize\tAccept\tSent\tRemaining"); {
for (int i = 0; i < n; i++) { distance[node] = MAX_VALUE;
int recv, sent; }
if (a[i] != 0) { distance[source] = 0;
if (buck_rem + a[i] > buck_cap) { for (int i = 1; i <= numVertices - 1; i++) {
recv = -1; // Dropped for (int src = 1; src <= numVertices; src++) {
} else { for (int dest = 1; dest <= numVertices; dest++) {
recv = a[i]; if (adjacencyMatrix[src][dest] != MAX_VALUE) {
buck_rem += a[i]; if (distance[dest] > distance[src] + adjacencyMatrix[src][dest]) {
} distance[dest] = distance[src] + adjacencyMatrix[src][dest];
} else { }
recv = 0; }
} }
if (buck_rem != 0) { }
if (buck_rem < rate) { }
sent = buck_rem; for (int src = 1; src <= numVertices; src++) {
buck_rem = 0; for (int dest = 1; dest <= numVertices; dest++) {
} else { if (adjacencyMatrix[src][dest] != MAX_VALUE) {
sent = rate; if (distance[dest] > distance[src] + adjacencyMatrix[src][dest]) {
buck_rem -= rate; [Link]("\n⚠ The graph contains a negative weight cycle!");
} return;
} else { }
sent = 0; }
} }
if (recv == -1) }
[Link]((i + 1) + "\t\t" + a[i] + "\tDropped\t\t" + sent + "\t" + buck_rem); [Link]("\nShortest distances from source vertex " + source + ":");
else for (int vertex = 1; vertex <= numVertices; vertex++) {
[Link]((i + 1) + "\t\t" + a[i] + "\t\t" + recv + "\t\t" + sent + "\t" + buck_rem); [Link]("Vertex " + source + " → " + vertex + " = " + distance[vertex]);
} }
} }
} public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of vertices: ");
int numVertices = [Link]();
int[][] adjacencyMatrix = new int[numVertices + 1][numVertices + 1];
[Link]("Enter the adjacency matrix:");
for (int src = 1; src <= numVertices; src++)
{
for (int dest = 1; dest <= numVertices; dest++)
{
adjacencyMatrix[src][dest] = [Link]();
if (src == dest)

{ Program – 03
adjacencyMatrix[src][dest] = 0; A Client – Server program
} else if (adjacencyMatrix[src][dest] == 0) {
adjacencyMatrix[src][dest] = MAX_VALUE; import [Link].*;
} import [Link].*;
} public class TCPS
} {
[Link]("Enter the source vertex: "); public static void main(String[] args) throws Exception {
int source = [Link](); ServerSocket sersock = new ServerSocket(4000);
Ford bellmanFord = new Ford(numVertices); [Link]("Server ready for connection...");
[Link](source, adjacencyMatrix); Socket sock = [Link]();
[Link](); [Link]("Connection successful. Waiting for file request...");
} BufferedReader fileRead = new BufferedReader(new InputStreamReader([Link]()));
} String fname = [Link]();
[Link]("Client requested file: " + fname);
BufferedReader contentRead = new BufferedReader(new FileReader(fname));
PrintWriter pwrite = new PrintWriter([Link](), true);
String str;
while ((str = [Link]()) != null) {
[Link](str);
}
[Link]("File transfer completed.");
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

import [Link].*;
import [Link].*;
public class TCPC
{
public static void main(String[] args) throws Exception {
Socket sock = new Socket("[Link]", 4000);
[Link]("Connected to Server.");
[Link]("Enter the filename:");
BufferedReader keyRead = new BufferedReader(new InputStreamReader([Link]));
String fname = [Link]();
PrintWriter pwrite = new PrintWriter([Link](), true);
[Link](fname);
BufferedReader socketRead = new BufferedReader(new InputStreamReader([Link]()));
String str;
[Link]("\n--- File Content Start ---");
while ((str = [Link]()) != null) {
[Link](str);
}
[Link]("--- File Content End ---");
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Program - 04
Datagram socket

import [Link].*;
import [Link].*;

public class ServerPgm {


public static void main(String[] args) throws Exception {
try (DatagramSocket sock = new DatagramSocket(9876);
BufferedReader in = new BufferedReader(new InputStreamReader([Link]))) {
[Link]("Waiting for client request...");
byte[] buf = new byte[1024];
while (true) {
DatagramPacket p = new DatagramPacket(buf, [Link]);
[Link](p);
String msg = new String([Link](), 0, [Link]());
[Link]("Client (" + [Link]() + "): " + msg);

[Link]("Server reply: ");


String reply = [Link]();
byte[] out = [Link]();
[Link](new DatagramPacket(out, [Link], [Link](), [Link]()));
[Link]("Message sent.\n");
}
}
}
}

import [Link].*;
import [Link].*;

public class ClientPgm {


public static void main(String[] args) throws Exception {
try (DatagramSocket sock = new DatagramSocket();
BufferedReader in = new BufferedReader(new InputStreamReader([Link]))) {
InetAddress addr = [Link]("[Link]");

[Link]("Client: ");
String msg = [Link]();
byte[] out = [Link]();
[Link](new DatagramPacket(out, [Link], addr, 9876));

byte[] buf = new byte[1024];


DatagramPacket p = new DatagramPacket(buf, [Link]);
[Link](p);
String reply = new String([Link](), 0, [Link]());
[Link]("Server Message received: " + reply);
}
}
}}

You might also like