7. Write a program for error detecting code using CRC-CCITT (16- bits).
import java.io.*;
import java.util.*;
public class CRC {
public static void main(String[] args)throws IOException {
Scanner input = new Scanner(System.in);
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=input.nextInt();
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=input.nextInt();
System.out.println("Enter number of bits in divisor : ");
divisor_bits=input.nextInt();
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=input.nextInt();
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ CRC GENERATION-----------------------*/
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++){
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++) //append dividend and ramainder
{
crc[i]=(div[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/*-------------------ERROR DETECTION---------------------*/
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=input.nextInt();
for(int j=0; j<crc.length; j++){
rem[j] = crc[j];
}
rem=divide(crc, divisor, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("Error");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}
System.out.println("THANK YOU.... :)");
}
static int[] divide(int div[],int divisor[], int rem[])
{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
while(rem[cur]==0 && cur!=rem.length-1)
cur++;
if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}
OUTPUT
CASE-1
Enter number of data bits :
5
Enter data bits :
1
0
1
1
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
0
1
Dividend (after appending 0's) are : 1011100
CRC code :
1011111
Enter CRC code of 7 bits :
1
0
1
1
1
0
0
Error
THANK YOU.... :)
CASE-2
Enter number of data bits :
5
Enter data bits :
1
1
0
1
1
Enter number of bits in divisor :
3
Enter Divisor bits :
1
1
1
Dividend (after appending 0's) are : 1101100
CRC code :
1101100
Enter CRC code of 7 bits :
1
1
0
1
1
0
0
No Error
THANK YOU.... :)
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
Enter the source vertex
0
distance of source 0 to 0 is 0
distance of source 0 to 1 is 4
distance of source 0 to 2 is 6
distance of source 0 to 3 is 1
distance of source 0 to 4 is -1
CASE-2
Enter the number of vertices
5
Enter the adjacency matrix
0 3 9999 6 3
9999 9999 9 4 9999
9999 9999 9999 9999 6
2 9999 9999 3 1
9999 9999 0 2 9999
Enter the source vertex
3
distance of source 3 to 0 is 2
distance of source 3 to 1 is 5
distance of source 3 to 2 is 14
distance of source 3 to 3 is 0
distance of source 3 to 4 is 1
9. Using 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.
// SERVER SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //
import java.net.*;
import java.io.*;
public class Server1 {
public static void main(String[] args)throws Exception
{
// establishing the connection with the server
ServerSocket sersock = new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket sock = sersock.accept(); // binding with port: 4000
System.out.println("Connection is successful and wating");
// reading the file name from client
InputStream istream = sock.getInputStream( );
BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
String fname = fileRead.readLine( );
// reading file contents
BufferedReader contentRead = new BufferedReader(new FileReader(fname) );
// keeping output stream ready to send the contents
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
String str;
while((str = contentRead.readLine()) != null) // reading line-by-line from file
{
pwrite.println(str); // sending each line to client
}
sock.close(); sersock.close(); // closing network sockets
pwrite.close(); fileRead.close(); contentRead.close();
}
}
// CLIENT SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args)throws Exception
{
Socket sock = new Socket( "127.0.0.1", 4000);
// reading the file name from keyboard. Uses input stream
System.out.print("Enter the file name");
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
String fname = keyRead.readLine();
// sending the file name to server. Uses PrintWriter
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(fname);
// receiving the contents from server. Uses input stream
InputStream istream = sock.getInputStream();
BufferedReader socketRead = new BufferedReader(new InputStreamReader(istream));
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
Server side: (First run the sever)
Server ready for connection
Connection is successful and waiting for chatting
Client side: (After the sever is ready, run the client program...... note file need to saved at the
sever side)
Enter the file name RIT.txt
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.
// SERVER SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception {
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket dsock = new DatagramSocket();
InetAddress address = InetAddress.getLocalHost( );
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);
}
}
}
// CLIENT SIDE PROGRAM (TYPE IN A DIFFERENT WORKSPACE) //
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws SocketException, IOException {
DatagramSocket dsock = new DatagramSocket(2000);
DatagramPacket dpack;
while(true)
{
byte arr1[] = new byte[100];
dpack = new DatagramPacket(arr1, arr1.length);
dsock.receive(dpack);
String str = new String(arr1);
System.out.println("Message recieved from server");
System.out.println(str);
}
}
}
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)
(Then come to Server program)
Server is ready
Enter message to be send to client from server
RIT, HASSAN.
(Above text is the entered message at server side)
(Then come to client Side)
Client Side: (it displays the below message)
Message received from server
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 {
public static void main(String[] args) {
BigInteger p,q,n,z,e,d;
byte [] encrypted=new byte[1000];
byte [] decrypted=new byte[1000];
int range=128;
Random random=new Random();
p=BigInteger.probablePrime(range,random);
q=BigInteger.probablePrime(range,random);
e=BigInteger.probablePrime(range, random);
n=p.multiply(q);
z=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
while(z.gcd(e).compareTo(BigInteger.ONE)>0&& e.compareTo(z)<0)
{
e.add(BigInteger.ONE);
}
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;
public static byte[] encrypt_decrypt(byte[] message,BigInteger e,BigInteger n,boolean t)
{
BigInteger c=new BigInteger(message).modPow(e,n);
if(t)
System.out.println("cipher text;"+c);
return c.toByteArray ();
}
}
OUTPUT:
enter the text
process
ASCII: 1121141119910111511532
Ciphertext;121242082357917429600093446984534937533130784677880713142468435838434
4 6587727
decrypted String:process
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);
System.out.println("enter the output rate");
int oprate=input.nextInt();
System.out.println("enter the bucket size");
int bktsize=input.nextInt();
for(int i=1;i<=5;i++)
System.out.println(" the packet number is "+i);
System.out.println("enter the packet size of "+i);
System.out.println();
int pktsize=input.nextInt();
System.out.println();
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
enter the output rate
50
enter the bucket size
500
the packet number is 1
enter the packet size of 1
250
50bytes outputted
50bytes outputted
50bytes outputted
50bytes outputted
last 50 bytes outputted
bucket output sucessfull
the packet number is 2
enter the packet size of 2
510
bucket overflow
the packet number is 3
enter the packet size of 3
175
50bytes outputted
50bytes outputted
50bytes outputted
last 25 bytes outputted
bucket output sucessfull
the packet number is 4
enter the packet size of 4
30
last 30 bytes outputted
bucket output sucessfull
the packet number is 5
enter the packet size of 5
55
50bytes outputted
last 5 bytes outputted
bucket output successful
CASE- 2
enter the output rate
30
enter the bucket size
100
the packet number is 1
enter the packet size of 1
50
30bytes outputted
last 20 bytes outputted
bucket output sucessfull
the packet number is 2
enter the packet size of 2
30
last 30 bytes outputted
bucket output sucessfull
the packet number is 3
enter the packet size of 3
50
30bytes outputted
last 20 bytes outputted
bucket output sucessfull
the packet number is 4
enter the packet size of 4
60
30bytes outputted
last 30 bytes outputted
bucket output sucessfull
the packet number is 5
enter the packet size of 5
60
30bytes outputted
last 30 bytes outputted
bucket output sucessfull
CASE- 3
enter the output rate
50
enter the bucket size
300
the packet number is 1
enter the packet size of 1
100
50bytes outputted
last 50 bytes outputted
bucket output sucessfull
the packet number is 2
enter the packet size of 2
150
50bytes outputted
50bytes outputted
last 50 bytes outputted
bucket output sucessfull
the packet number is 3
enter the packet size of 3
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
last 30 bytes outputted
bucket output sucessfull