Partb Java Programs
Partb Java Programs
import java.io.*;
import java.util.*;
public class CRC {
CASE-1
1
0
1
1
1
0
0
Error
THANK YOU.... :)
CASE-2
8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.
import java.util.Scanner;
public class Bellman {
private int d[];
private int n;
public static final int MAX= 9999;
public static void main(String[] args) {
int n=0,s;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
n = scanner.nextInt();
int a[][] = new int[n][n];
System.out.println("Enter the adjacency matrix");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = scanner.nextInt();
if (a[i][j] == 0)
{
a[i][j] = MAX;
}
}
}
System.out.println("Enter the source vertex");
s = scanner.nextInt();
Bellman b= new Bellman(n);
b.Bellmanford(s,a);
scanner.close();
}
public Bellman(int n)
{
this.n = n;
d = new int[n + 1];
}
public void Bellmanford(int s, int a[][])
{
for (int i = 0; i < n; i++)
{
d[i] = MAX;
}
d[s] = 0;
for (int k = 0; k < n - 1; k++)
{ for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{ if (a[i][j] != MAX)
{ if (d[j] > d[i]+ a[i][j])
{ d[j] = d[i]+ a[i][j];
}
}
}
}
}
for (int i = 0; i <n; i++)
{
System.out.println("distance of source " + s + " to "+ i + " is " + d[i]);
}
}
}
OUTPUT
CASE-1
Enter the number of vertices
5
Enter the adjacency matrix
0 4 6 9999 9999
9999 0 8 -3 -5
9999 9999 0 -2 11
9999 9999 9999 0 9999
3 9999 9999 9999 9999
CASE-2
import java.net.*;
import java.io.*;
public class Server1 {
String str;
while((str = contentRead.readLine()) != null) // reading line-by-line from file
{
pwrite.println(str); // sending each line to client
}
import java.net.*;
import java.io.*;
public class Client {
String str;
while((str = socketRead.readLine()) != null) // reading line-by-line
{
System.out.println(str);
}
pwrite.close();
socketRead.close();
keyRead.close();
}
}
OUTPUT:
Note: As we are using eclipse save and run the server and client program in different
workspace
Client side: (After the sever is ready, run the client program...... note file need to saved at the
sever side)
RIT.
Dept. of CSE
Computer Network Laboratory
15CSL57
( contents of the file name RIT.txt will be displayed as shown above)
10. Write a program on datagram socket for client/server to display the messages on client
side, typed at the server side.
import java.io.*;
import java.net.*;
public class Server {
System.out.println("Server is ready");
while(true)
{
Thread.sleep(1000);
System.out.println("Enter message to be send to client from server ");
String s1 =buff.readLine();
byte arr[] = s1.getBytes( );
DatagramPacket dpack = new DatagramPacket(arr, arr.length, address, 2000);
dsock.send(dpack);
}
}
}
import java.io.*;
import java.net.*;
dsock.receive(dpack);
}
}
}
OUTPUT:
Note: As we are using eclipse save and run the server and client program in different
workspace
Server side: (First run the sever)
Server is ready
Enter message to be send to client from server
Client side: (After the sever is ready, run the client program)
Server is ready
Enter message to be send to client from server
RIT, HASSAN.
RIT, HASSAN.
11. Write a program for simple RSA algorithm to encrypt and decrypt the data.
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
public class Rsa {
}
d=e.modInverse(z);
System.out.println("d:"+d+"\nn"+n);
Scanner in=new Scanner(System.in);
String text;
System.out.println("enter the text");
text=in.nextLine();
System.out.println("ASCII:"+BytestoString(text.getBytes ()));
encrypted=encrypt_decrypt(text.getBytes(), e,n,true);
decrypted=encrypt_decrypt(encrypted,d,n,false);
System.out.println("decrypted String:"+new String(decrypted));
}
public static String BytestoString(byte[] encrypted)
{
String test=" ";
for(int i=0;i<encrypted.length;i++)
{
test=test+encrypted[i]+"";
}
return test;
OUTPUT:
12. Write a program for congestion control using leaky bucket algorithm.
import java.util.Scanner;
public class Leakybuckett {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
for(int i=1;i<=5;i++)
if(pktsize>bktsize)
System.out.println(" bucket overflow");
else
{
while(pktsize>oprate)
{
System.out.println(oprate+ "bytes outputted ");
try {
Thread.sleep(1000);
} catch (InterruptedException ie)
{
//Handle exception
}
System.out.println();
pktsize=pktsize-oprate;
if(pktsize>0)
{
System.out.println("last " +pktsize+ " bytes outputted");
System.out.println("bucket output sucessfull");
System.out.println();
}
}
}
OUTPUT:
CASE-1
250
50bytes outputted
50bytes outputted
50bytes outputted
50bytes outputted
510
bucket overflow
the packet number is 3
enter the packet size of 3
175
50bytes outputted
50bytes outputted
50bytes outputted
30
55
50bytes outputted
CASE- 2
50
30bytes outputted
30
50
30bytes outputted
60
30bytes outputted
60
30bytes outputted
CASE- 3
100
50bytes outputted
150
50bytes outputted
50bytes outputted
350
bucket overflow
the packet number is 4
enter the packet size of 4
400
bucket overflow
the packet number is 5
enter the packet size of 5
180
50bytes outputted
50bytes outputted
50bytes outputted