Skip to content

Commit fe07f20

Browse files
authored
initial commit
0 parents  commit fe07f20

File tree

5 files changed

+356
-0
lines changed

5 files changed

+356
-0
lines changed

BFAlgo.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//Write a program to find the shortest path between vertices using bellman-ford algorithm.
2+
3+
import java.util.*;
4+
import java.io.*;
5+
import java.math.*;
6+
7+
class BFAlgo
8+
{
9+
public static void main(String args[])
10+
{
11+
Scanner s=new Scanner(System.in);
12+
13+
//Input the number of nodes in the graph
14+
System.out.println("Enter the number of nodes: ");
15+
int n=s.nextInt();
16+
17+
//Input source and destination node numbers
18+
System.out.println("Enter Source node number :");
19+
int source=s.nextInt();
20+
21+
System.out.println("Enter Destination node number :");
22+
int dest=s.nextInt();
23+
24+
//create distance array
25+
int[] dist=new int[n];
26+
for(int i=0;i<n;i++)
27+
//Initialize the array with a maximum value which is assumed to be far greater then any edge weight
28+
dist[i]=999;
29+
30+
//create adj matrix
31+
int[][] adj_mat=new int[n][n];
32+
System.out.println("Enter adj matrix :");
33+
34+
//Input the adj_matrix
35+
for(int i=0;i<n;i++)
36+
{
37+
for(int j=0;j<n;j++)
38+
{
39+
40+
adj_mat[i][j]=s.nextInt();
41+
42+
}
43+
}
44+
45+
//display adj_mat
46+
System.out.println("\n\nEntered ADJ MATRIX IS:");
47+
disp(adj_mat);
48+
dist[source]=0;
49+
50+
//loop through all the nodes
51+
for(int k=0;k<n-1;k++)
52+
{
53+
for(int i=0;i<n;i++)
54+
{
55+
//for each node as the souce update the cost in adj_matrix
56+
for(int j=0;j<n;j++)
57+
{
58+
//if there is a direct path between nodes i and j
59+
if(adj_mat[j][i]!=999)
60+
{
61+
//then check if the direct path is shorter than an alternate path through another node
62+
//choose the shortest dist and update the distance array
63+
if(dist[i]>dist[j]+adj_mat[j][i])
64+
dist[i]= dist[j]+adj_mat[j][i];
65+
}
66+
}
67+
68+
69+
}
70+
}
71+
72+
//display statements
73+
for(int i=0;i<n;i++)
74+
System.out.println(source+"-->"+i+" = "+dist[i]);
75+
}
76+
77+
//helper function for 2D matrix
78+
public static void disp(int arr[][])
79+
{
80+
for(int[] x : arr)
81+
{ for(int y : x)
82+
System.out.print(y);
83+
System.out.println();
84+
}
85+
}
86+
87+
88+
}

Bucket.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//Write a program for congestion control using leaky bucket algorithm.
2+
3+
import java.security.SecureRandom;
4+
import java.util.*;
5+
import java.io.*;
6+
7+
class Bucket
8+
{
9+
public static void main(String args[])
10+
{
11+
Random r=new Random();
12+
Scanner s = new Scanner(System.in);
13+
14+
System.out.println("Enter the input rate: ");
15+
int n=s.nextInt();
16+
17+
System.out.println("Enter the Bucket Size: ");
18+
int bucket_size=s.nextInt();
19+
20+
System.out.println("Enter the Output Rate: ");
21+
int out_rate=s.nextInt();
22+
23+
System.out.println("Enter packet data: ");
24+
int[] input_arr=new int[n];
25+
26+
for(int i=0;i<n;i++)
27+
{
28+
input_arr[i]=r.nextInt(127);//s.nextInt(); here we'll be auto populating the array instead of inputting n numbers
29+
}
30+
31+
int bc=0;//bucket_content=0
32+
33+
//loop through for each packet in input_arr[]
34+
for(int i=0;i<n;i++)
35+
{
36+
//check if the new packet fits into the bucket at the current instance
37+
if(bc+input_arr[i]<=bucket_size)
38+
{ //if new packet can be fit then insert it into the bucket
39+
bc=bc+input_arr[i];
40+
System.out.print(i+"\tPacket: "+input_arr[i]+"\tBucket Content: "+bc+"\tStatus: Accepted\tRemaining: ");
41+
//after processing(displaying) the status display remove bucket content equal to out_rate
42+
bc=bc-out_rate;
43+
44+
//note - removal of element must not cause negative bucket capacity.
45+
if(bc<=0)//bc=bc<=0?0:bc;
46+
{
47+
bc=0;
48+
}
49+
System.out.println(bc);//Display remaining capacity
50+
}
51+
//If the packet does not fit into current bucket then simply drop the packet and continue to out_rate
52+
else
53+
{
54+
System.out.print(i+"\tPacket: "+input_arr[i]+"\tBucket Content: "+bc+"\tStatus: Rejected\tRemaining: ");
55+
//remove outrate from bc as normal cycle
56+
bc=bc-out_rate;
57+
if(bc<=0)//dont let the bucket capacity assume negative values
58+
{
59+
bc=0;
60+
}
61+
System.out.println(bc);
62+
}
63+
64+
65+
66+
}
67+
68+
}
69+
70+
}

ED.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import java.math.*;
2+
import java.io.*;
3+
import java.security.*;
4+
/*
5+
ALGORITHM
6+
==========
7+
STEP 1 > pick 2 large prime numbers , say 'p' and 'q'
8+
9+
STEP 2 > Compute their product, say n = p * q
10+
11+
STEP 3 > Compute PHI(n) as PHI(n) = (p-1) * (q-1)
12+
13+
STEP 4 > pick a value 'e' such that GCD(e,PHI(n))=1 , here 'e' is the encrytion key
14+
15+
STEP 5 > calculate 'd' as d = e^(-1) mod PHI(n) , here 'd' is the decryption key
16+
17+
STEP 6 > now we have our PublicKey=(e,n) and PrivateKey=(d,n)
18+
19+
STEP 7 > Encryption : CipherText = (Message)^e mod n
20+
21+
STEP 8 > Decryption : PlainMsg = (CipherText)^d mod n
22+
23+
*/
24+
25+
class ED_helper
26+
{
27+
private int bitlen,r;
28+
private BigInteger p,q,n,phi,e,d;
29+
30+
ED_helper(int bit)
31+
{
32+
bitlen=bit;//number of bits strong we want our encryption to be
33+
SecureRandom r=new SecureRandom();//we use this because its a 128bit rand_generator,hence low chances of repeats.
34+
35+
//<================================< STEP 1 >======================================>
36+
37+
//SYNTAX
38+
//BigInteger(int bitLength, int certainty, Random rnd)
39+
40+
//we want to generate encryption keys as a product of p and q hence each of p and q much contain half as many digits
41+
//as bitlen.And the second value to the BigInt tells us we want to generate primes with 100% certainity using 'r'
42+
p=new BigInteger(bitlen/2,100,r);
43+
q=new BigInteger(bitlen/2,100,r);
44+
45+
System.out.println("P value :"+p+"\n\n\n "+"Q value="+q);
46+
47+
//<================================< STEP 2 >======================================>
48+
n=p.multiply(q);//BigInteger Multiplication
49+
50+
//<================================< STEP 3 >======================================>
51+
phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
52+
53+
//<================================< STEP 4 >======================================>
54+
e=new BigInteger(bitlen/2,100,r);//BigInteger will take care of GCD() <UNDERATED STEP>
55+
56+
//<================================< STEP 5 >======================================>
57+
d=e.modInverse(phi);//e^-1 then % phi
58+
59+
//<================================< STEP 6 >======================================>
60+
System.out.println("\n\n\nValue of D :"+d+"\n\n\nValue of E : "+e);//Displaying keys
61+
62+
}
63+
64+
65+
//<================================< STEP 7 >======================================>
66+
public BigInteger encrypt(BigInteger Msg)
67+
{
68+
//CipherText = (Msg)^e mod n
69+
return(Msg.modPow(e,n));
70+
71+
}
72+
73+
//<================================< STEP 8 >======================================>
74+
public BigInteger decrypt(BigInteger Msg)
75+
{
76+
//DecodedMsg = (Ciphext)^d mod n
77+
return(Msg.modPow(d,n));
78+
79+
}
80+
81+
82+
}
83+
84+
//Main Driver Class to demonstrate functionalities implemented by the above class
85+
class ED
86+
{
87+
88+
public static void main(String args[]) throws IOException
89+
{
90+
//Create object and pass the bit length
91+
ED_helper rsa=new ED_helper(1200);
92+
93+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
94+
95+
String text1,text2;
96+
BigInteger Pt,Ct;
97+
98+
//Input Message from user
99+
System.out.println("\n\nEnter Plaintext :");
100+
text1=br.readLine();
101+
102+
//pass the input message to the encrypt method to obtain cipher text
103+
Pt=new BigInteger(text1.getBytes());
104+
Ct=rsa.encrypt(Pt);
105+
System.out.println("\n\nCiperText is : "+Ct);
106+
107+
//Pass the CipherText to the decrypt method to obtain original message
108+
Pt=rsa.decrypt(Ct);
109+
text2=new String(Pt.toByteArray());
110+
System.out.println("\n\n\nData after decrypt :"+text2);
111+
}
112+
113+
}

tcpClient.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.net.*;
2+
import java.io.*;
3+
4+
class tcpClient
5+
{
6+
public static void main(String args[]) throws IOException
7+
{
8+
//Establish a connection at the given IP and port number. "localhost" refers to this computer
9+
Socket sock=new Socket("localhost",5050);
10+
11+
//Input the file name to be retrived from the server
12+
System.out.println("Enter filename :");
13+
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
14+
String fname=br.readLine();
15+
16+
//create output stream to send data to outstream
17+
OutputStream ostream=sock.getOutputStream();
18+
PrintWriter pw = new PrintWriter(ostream,true);
19+
//send filename to the server by writing to outstream using PrintWriter
20+
pw.println(fname);
21+
22+
//Setup input strem to recieve file contents from the server
23+
InputStream istream=sock.getInputStream();
24+
BufferedReader sockread=new BufferedReader(new InputStreamReader(istream));
25+
String msg;
26+
27+
//Store and print out each line that was read from the inputstream
28+
while((msg=sockread.readLine())!=null)
29+
{
30+
System.out.println(msg);
31+
}
32+
33+
}
34+
}

tcpServer.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//TCP Server
2+
3+
import java.net.*;
4+
import java.io.*;
5+
6+
class tcpServer
7+
{
8+
9+
public static void main(String args[]) throws IOException
10+
{
11+
//setup a server socket on a port no(needs to be greater than 1023) , say 5050
12+
ServerSocket sersock=new ServerSocket(5050);
13+
System.out.println("Server Started");
14+
15+
//Once a client tries to connect to the server we accept the connection
16+
Socket sock=sersock.accept();
17+
System.out.println("Server ready");
18+
19+
//Create an input stream to read the information sent by the client
20+
InputStream istream=sock.getInputStream();
21+
22+
//Use a BufferedReader to read the contents from the above created input stream
23+
BufferedReader br=new BufferedReader(new InputStreamReader(istream));
24+
25+
//read and store the contents from the istream into a string variable 'filename'
26+
String fname=br.readLine();
27+
28+
//the contents of the specified filename passed by the client is read here(at the server) using another bufferedReader
29+
BufferedReader ContentReader=new BufferedReader(new FileReader(fname));
30+
31+
//An output stream is created so as to realy the data the client had requested for
32+
OutputStream ostream=sock.getOutputStream();
33+
34+
//create a PrintWriter class to write the contents of the requested file into the outstream
35+
PrintWriter pr=new PrintWriter(ostream,true);//SYNTAX : PrintWriter(Writer out, boolean autoFlush
36+
37+
//msg is a temporary variable to store and send data to the outstream via printwriter
38+
String msg;
39+
System.out.println("Sending contents of "+fname);
40+
//Keep sending data from file to outstream until EOF/Null is encountered
41+
while((msg=ContentReader.readLine()) != null)
42+
{
43+
pr.println(msg);
44+
}
45+
46+
sock.close();
47+
}
48+
49+
50+
51+
}

0 commit comments

Comments
 (0)