You are on page 1of 7

Implement the following in Java: ( PART – B )

7. Write a program for error detecting code using CRC-CCITT (16- bits).

import java.util.*;

class Crcccitt16
{
public String Crc(String dividend,String divisor)
{
String str1,div;
while(divisor.length()<=dividend.length())
{
div=dividend.substring(0,divisor.length());

str1=Integer.toBinaryString(Integer.parseInt(div,2)^Integer.parseInt(divisor,2));
int j=divisor.length();
str1+=dividend.substring(j,dividend.length());
dividend=str1;
}
return dividend;
}
public static void main(String args[]) {
String data,checksum,syn,dividend,received_data;
int padding;
String polynomial="10001000000100001";
Scanner sc=new Scanner(System.in);
System.out.print("Enter the data to be encrypted : ");
data=sc.next();
dividend=data;
padding =polynomial.length()-1;
for(int i=0;i<padding;i++)
{
dividend+="0";
}
Crcccitt16 obj=new Crcccitt16();
checksum=obj.Crc(dividend,polynomial);
data=data+checksum;
System.out.println("Sender checksum : "+checksum);
System.out.println("Codeword transmitted over network : "+data);
System.out.print("Enter the received codeword : ");
received_data=sc.next();
syn=obj.Crc(received_data,polynomial);
if(Long.parseLong(syn)==0)
System.out.println("No error in data transmission.");
else
System.out.println("Error in transmission.");
sc.close();
}
}

Output : Tset 1

Enter the data to be encrypted : 1000


Sender checksum : 1000000100001000
Codeword transmitted over network : 10001000000100001000
Enter the received codeword : 10001000000100001000
No error in data transmission.

Output : Test 2

Enter the data to be encrypted : 1000


Sender checksum : 1000000100001000
Codeword transmitted over network : 10001000000100001000
Enter the received codeword : 10001000000100001011
Error in transmission.
8. Write a program to find the shortest path between vertices using bellman-ford
algorithm.

import java.util.Scanner;

public class BellmanFord {


private int D[];
private int num_ver;
public static final int MAX_VALUE = 999;
public BellmanFord(int num_ver) {
this.num_ver = num_ver;
D = new int[num_ver + 1];
}
public void BellmanFordEvaluation(int source, int A[][]) {
for (int node = 1; node <= num_ver; node++) {
D[node] = MAX_VALUE;
}
D[source] = 0;
for (int node = 1; node <= num_ver - 1; node++) {
for (int sn = 1; sn <= num_ver; sn++) {
for (int dn = 1; dn <= num_ver; dn++) {
if (A[sn][dn] != MAX_VALUE) {
if (D[dn] > D[sn] + A[sn][dn])
D[dn] = D[sn] + A[sn][dn];
}
}
}
}
for (int sn = 1; sn <= num_ver; sn++) {
for (int dn = 1; dn <= num_ver; dn++) {
if (A[sn][dn] != MAX_VALUE) {
if (D[dn] < D[sn] + A[sn][dn])
System.out.println("The graph contains negative edge cycle");
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++) {
System.out.println("Distance of source: " + source + " to " + vertex + " is " +
D[vertex]);
}
}
public static void main(String[] args) {
int num_ver = 0;
int source;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the no of vertices: ");
num_ver = scanner.nextInt();
int A[][] = new int[num_ver + 1][num_ver + 1];
System.out.println("Enter the adjacency matrix: ");
for (int sn = 1; sn <= num_ver; sn++) {
for (int dn = 1; dn <= num_ver; dn++) {
A[sn][dn] = scanner.nextInt();
if (sn == dn) {
A[sn][dn] = 0;
continue;
}
if (A[sn][dn] == 0) {
A[sn][dn] = MAX_VALUE;
}
}
}
System.out.print("Enter the source vertex: ");
source = scanner.nextInt();
BellmanFord b = new BellmanFord(num_ver);
b.BellmanFordEvaluation(source, A);
scanner.close();
}
}
Output :

Enter the no of vertices: 6


Enter the adjacency matrix:
0 9 0 6 8 0
0 0 0 0 5 4
10 0 0 -1 0 0
6 0 0 0 -2 0
0 0 0 0 0 6
0 0 0 0 0 0
Enter the source vertex: 4
The graph contains negative edge cycle
The graph contains negative edge cycle
The graph contains negative edge cycle
The graph contains negative edge cycle
The graph contains negative edge cycle
The graph contains negative edge cycle
Distance of source: 4 to 1 is 6
Distance of source: 4 to 2 is 15
Distance of source: 4 to 3 is 999
Distance of source: 4 to 4 is 0
Distance of source: 4 to 5 is -2
Distance of source: 4 to 6 is 4

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.

//TCP_Server
import java.io.*;
import java.net.*;
public class TCP_Server
{
public static void main(String args[])
throws Exception
{
ServerSocket sc=new ServerSocket(1534);
Socket q=sc.accept();
System.out.println("connection establish successfully ");
BufferedReader v=new BufferedReader(new InputStreamReader(q.getInputStream()));
DataOutputStream dr=new DataOutputStream(q.getOutputStream());
String g=v.readLine();
FileReader f=null;
BufferedReader ff=null;
Boolean b;
File r=new File(g);
if(r.exists())
b=true;
else
b=false;
if(b==true)dr.writeBytes("yes"+"\n");
else
b=false;
if(b==true)dr.writeBytes("yes"+"\n");
else
dr.writeBytes("no"+"\n");
if(b==true)
{
f=new FileReader(g);
ff=new BufferedReader(f);
String qq;
while((qq=ff.readLine()) !=null)
{
dr.writeBytes(qq+"\n");
}
dr.close();
ff.close();
v.close();
sc.close();
q.close();
f.close();
}
}
}

//TCP_Client
import java.io.*;
import java.net.*;

class TCP_Client{
public static void main(String args[])throws Exception{
Socket s = new Socket("localhost",1534);
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
System.out.print("nter the file name : ");
String filename = k.readLine();
DataOutputStream d = new DataOutputStream(s.getOutputStream());
d.writeBytes(filename+"\n");
BufferedReader i = new BufferedReader(new InputStreamReader(s.getInputStream()));
String st;
st = i.readLine();
if(st.equals("yes"))
{
while((st = i.readLine()) != null)
System.out.println(st);
i.close();
d.close();
k.close();
}else
System.out.println("file not found");
}
}

Output : Test 1

CMD - 01
G:\CSE-BE\SEM\5th Sem\Lab\CN\devil>java TCP_Server
connection establish successfully

CMD - 02
G:\CSE-BE\SEM\5th Sem\Lab\CN\devil>java TCP_Client
nter the file name : Devil.txt
yes
HI partner

Output : Test 2

CMD - 01
G:\CSE-BE\SEM\5th Sem\Lab\CN\devil>java TCP_Server
connection establish successfully

CMD - 02
G:\CSE-BE\SEM\5th Sem\Lab\CN\devil>java TCP_Client
nter the file name : DevilDe
file not found

10. Write a program on datagram socket for client/server to display the messages on
client side, typed at the server side.

//UDP_Server
import java.io.*;
import java.net.*;
import java.util.*;

class UDP_Server
{
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new DatagramSocket(9876);
System.out.println("Server Started at port - 9876");
byte[] recieveData = new byte[1024];
byte[] sendData = new byte[1024];
DatagramPacket recievePacket = new DatagramPacket(recieveData,recieveData.length);
serverSocket.receive(recievePacket);
recievePacket.getData();
InetAddress IPAddress = recievePacket.getAddress();
int port = recievePacket.getPort();
System.out.println("Client Connected");
Scanner input = new Scanner(System.in);
System.out.print("Enter the message to be sent : ");
String message = input.nextLine();
sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,port);
serverSocket.send(sendPacket);
System.exit(0);
}
}

//UDP_Client
import java.io.*;
import java.net.*;
class UDP_Client
{
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] recieveData = new byte[1024];
byte[] sendData = new byte[1024];
System.out.println("Enter 'START' to connect to Server.");
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket recievePacket = new DatagramPacket(recieveData,recieveData.length);
clientSocket.receive(recievePacket);
String modifiedSentence = new String(recievePacket.getData());
System.out.println("Message received from server : "+modifiedSentence);
clientSocket.close();
}
}

Output : Test 1

CMD - 01
G:\CSE-BE\SEM\5th Sem\Lab\CN\devil>java UDP_Server
Server Started at port - 9876
Client Connected
Enter the message to be sent : HI Partner

CMD - 02
G:\CSE-BE\SEM\5th Sem\Lab\CN\devil>java UDP_Client
Enter 'START' to connect to Server.
START
Message received from server : HI Partner

11. Write a program for simple RSA algorithm to encrypt and decrypt the data.

import java.util.*;
import java.math.BigInteger;
import java.lang.*;
public class Rsa
{
public static void main(String args[])
{
Random rand1=new Random(System.currentTimeMillis());
Random rand2=new Random(System.currentTimeMillis()*10);
int pubkey=2;
BigInteger p=BigInteger.probablePrime(32,rand1);
BigInteger q=BigInteger.probablePrime(32,rand2);
BigInteger n=p.multiply(q);
BigInteger p_1=p.subtract(new BigInteger("1"));
BigInteger q_1=q.subtract(new BigInteger("1"));
BigInteger z=p_1.multiply(q_1);
while(true)
{
BigInteger GCD=z.gcd(new BigInteger(""+pubkey));
if(GCD.equals(BigInteger.ONE))
{
break;
}
pubkey++;
}
BigInteger big_pubkey=new BigInteger(""+pubkey);
BigInteger prvkey=big_pubkey.modInverse(z);
System.out.println("Public key : "+big_pubkey+","+n);
System.out.println("Private key : "+prvkey+","+n);
Scanner sc=new Scanner(System.in);
System.out.print("Enter the msg to be encrypted : ");
String msg=sc.next();
System.out.println();
byte[]bytes=msg.getBytes();
for(int i=0;i<msg.length();i++)
{
int asciival=bytes[i];
BigInteger val=new BigInteger(""+asciival);
BigInteger cipherval=val.modPow(big_pubkey,n);
System.out.println("Cipher text : "+cipherval);
BigInteger plainval=cipherval.modPow(prvkey,n);
int i_plainval=plainval.intValue();
System.out.println("Plaintext :
"+Character.toString((char)i_plainval)+"\n");
}
}
}

Output :

Public key : 5,11407077235276260953


Private key : 6844246337071056701,11407077235276260953
Enter the msg to be encrypted : devild

Cipher text : 10000000000


Plaintext : d

Cipher text : 10510100501


Plaintext : e

Cipher text : 22877577568


Plaintext : v

Cipher text : 12762815625


Plaintext : i

Cipher text : 14693280768


Plaintext : l

Cipher text : 10000000000


Plaintext : d

12. Write a program for congestion control using leaky bucket algorithm.

import java.util.*;

class CongestionControl
{
public static void main(String [] args)
{
int time,output_rate,max_buffer_size,num_of_pkts,count=0,cur_buffer_size=0;
Scanner in=new Scanner(System.in);
System.out.print("Enter max size of buffer : ");
max_buffer_size=in.nextInt();
System.out.print("Enter output rate of packets from the buffer : ");
output_rate=in.nextInt();
System.out.print("Enter the number of arriving packet : ");
num_of_pkts=in.nextInt();
int[] pkt_size=new int[num_of_pkts];
int[] arr_time_of_pkts=new int[num_of_pkts];
System.out.print("Enter time of arrival of packet : ");
for(count=0;count<num_of_pkts;count++)
{
arr_time_of_pkts[count]=in.nextInt();
}
time=0;
count=0;
while(count<num_of_pkts)
{
if(time==arr_time_of_pkts[count])
{
Random rn=new Random();
pkt_size[count]=(rn.nextInt(10)+1)*10;
System.out.println("Packet "+(count)+" has arrived and it's size is: "+pkt_size[count]);
System.out.println("Current size of buffer: "+cur_buffer_size);
if(cur_buffer_size+pkt_size[count]<max_buffer_size)
{
cur_buffer_size+=pkt_size[count];
System.out.println("Packet "+(count+1)+" arriving at "+arr_time_of_pkts[count]+" is
confirming.\n");
}
else
{
System.out.println("Packet "+(count+1)+" arriving at "+arr_time_of_pkts[count]+" is
non confirming packet as it exceeds the buffer time.\n");
}
count++;
}
time++;
cur_buffer_size=output_rate;
if(cur_buffer_size<0)
cur_buffer_size=0;
}
}
}

Output :

Enter max size of buffer : 100


Enter output rate of packets from the buffer : 40
Enter the number of arriving packet : 3
Enter time of arrival of packet : 1 1 1
Packet 0 has arrived and it's size is: 30
Current size of buffer: 40
Packet 1 arriving at 1 is confirming.

Packet 1 has arrived and it's size is: 60


Current size of buffer: 40
Packet 2 arriving at 1 is non confirming packet as it exceeds the buffer time.

Packet 2 has arrived and it's size is: 70


Current size of buffer: 40
Packet 3 arriving at 1 is non confirming packet as it exceeds the buffer time.

You might also like