CN Part B
CN Part B
import java.io.*;
class CRC
{ public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int divi[],gen[],divisor[],rem[],crc[],d, b, newlen;
System.out.println("Enter number of bits in bit stream : ");
d=Integer.parseInt(br.readLine());
divi=new int[d];
System.out.println("Enter bits of bit stream : ");
for(int i=0; i<d; i++)
divi[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in generator : ");
b=Integer.parseInt(br.readLine());
divisor=new int[b];
System.out.println("Enter generator bits : ");
for(int i=0; i<b; i++)
divisor[i]=Integer.parseInt(br.readLine());
newlen=d+b-1;
gen=new int[newlen];
rem=new int[newlen];
crc=new int[newlen];
for(int i=0;i<divi.length;i++)
gen[i]=divi[i];
System.out.print("Bit Sequence after appending zeros ");
for(int i=0; i< gen.length; i++)
System.out.print(gen[i]);
System.out.println();
for(int j=0; j<gen.length; j++){
rem[j] = gen[j];
}
rem=crc_divide(gen, divisor, rem);
for(int i=0;i<gen.length;i++)
{
crc[i]=(gen[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
System.out.println();
System.out.println("DETECTION OF ERROR : ");
System.out.println("Enter the crc code recived : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
for(int j=0; j<crc.length; j++){
rem[j] = crc[j];
}
rem=crc_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");
}
}
static int[] crc_divide(int div[],int divisor[], int rem[])
{
int temp=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[temp+i]=(rem[temp+i]^divisor[i]);
if((rem.length-temp)<divisor.length)
break;
}
return rem;
}
}
2-BELLMANFORD
import java.util.Scanner;
public class BellmanFord
{
int d[];
int n;
public static final int max = 999;
public BellmanFord(int n)
{
this.n = n;
d = new int[n + 1];
}
public void path(int source, int c[][])
{
for (int i = 1; i <= n; i++)
{
d[i] = max;
}
d[source] = 0;
for (int i = 1; i <= n - 1; i++)
{
for (int u = 1; u <= n; u++)
{
for (int v = 1; v <= n; v++)
{
if (c[u][v] != max)
{
if (d[u] + c[u][v] < d[v])
d[v] = d[u] + c[u][v];
}
}
}
}
// for (int u = 1; u <= n; u++)
// {
// for (int v = 1; v <= n; v++)
// {
// if (c[u][v] != max)
// {
// if (d[v] > d[u] + c[u][v])
// System.out.println("The Graph contains negative
egde cycle");
// }
// }
// }
for (int i = 1; i <= n; i++)
{
System.out.println("distance of source " + source + " to " + i
+ " is " + d[i]);
}
}
public static void main(String[] args)
{
int n = 0;
int source;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices");
n = scanner.nextInt();
int c[][] = new int[n + 1][n + 1];
System.out.println("Enter the adjacency matrix");
for (int u = 1; u <= n; u++) {
for (int v = 1; v <= n; v++) {
c[u][v] = scanner.nextInt();
if (u == v)
{
c[u][v] = 0;
continue;
}
if (c[u][v] == 0)
{
c[u][v] = max;
}
}
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BellmanFord b = new BellmanFord(n);
b.path(source, c);
scanner.close();
}
}
3)TCPIP
CLIENT
package labprogram_cn;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
class tcpclient
{
public static void main(String args[])throws Exception
{
String address = "";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Server Address: ");
address=sc.nextLine();
//create the socket on port 5000
Socket s=new Socket(address,5000);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Send Get to start...");
String str="",filename="";
try
{
while(!str.equals("start"))
str=br.readLine();
dout.writeUTF(str);
dout.flush();
filename=din.readUTF();
System.out.println("Receving file: "+filename);
filename="client"+filename;
System.out.println("Saving as file: "+filename);
long sz=Long.parseLong(din.readUTF());
System.out.println ("File Size: "+(sz/(1024*1024))+" MB");
byte b[]=new byte [1024];
System.out.println("Receving file..");
FileOutputStream fos=new FileOutputStream(new
File(filename),true);
long bytesRead;
do
{
bytesRead = din.read(b, 0, b.length);
fos.write(b,0,b.length);
}while(!(bytesRead<1024));
System.out.println("Comleted");
fos.close();
dout.close();
s.close();
sc.close();
}
catch(EOFException e)
{
//do nothing
}
}
}
SERVER
package labprogram_cn;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class tcpserver
{
public static void main(String args[])throws Exception
{
String filename;
System.out.println("Enter the File Name: ");
Scanner sc=new Scanner(System.in);
filename=sc.nextLine();
sc.close();
while(true)
{
//create server socket on port 5000
ServerSocket ss=new ServerSocket(5000);
System.out.println ("Waiting for request");
Socket s=ss.accept();
System.out.println ("Connected
With"+s.getInetAddress().toString());
DataInputStream din=new
DataInputStream(s.getInputStream());
DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
try
{
String str="";
str=din.readUTF();
System.out.println("SendGet....Ok");
if(!str.equals("stop"))
{
System.out.println("Sending File: "+filename);
dout.writeUTF(filename);
dout.flush();
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
long sz=(int) f.length();
byte b[]=new byte [1024];
int read;
dout.writeUTF(Long.toString(sz));
dout.flush();
System.out.println ("Size: "+sz);
System.out.println ("Buf size:"+ss.getReceiveBufferSize());
while((read = fin.read(b)) != -1)
{
dout.write(b, 0, read);
dout.flush();
}
fin.close();
System.out.println("..ok");
dout.flush();
}
dout.writeUTF("stop");
System.out.println("Send Complete");
dout.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("An error occured");
}
din.close();
s.close();
ss.close();
}
}
}
4)UDP
SERVER
package labprogram_cn;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
class server {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
DatagramSocket datagramSocket = new
DatagramSocket();
InetAddress clientAddress =
InetAddress.getByName("127.0.0.1");
String line;
byte[] buffer;
DatagramPacket datagramPacket;
System.out.println("Enter Messages to Send");
while (true) {
line = scanner.nextLine();
buffer = line.getBytes();
datagramPacket = new DatagramPacket(buffer,
buffer.length, clientAddress, 1234);
datagramSocket.send(datagramPacket);
if (line.equalsIgnoreCase("exit")) {
datagramSocket.close();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
scanner.close();
}
}
CLIENT
package labprogram_cn;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class cl {
public static void main(String[] args) {
try {
DatagramSocket datagramSocket = new
DatagramSocket(1234);
byte[] buffer;
DatagramPacket datagramPacket;
System.out.println("Messages Received");
while (true) {
buffer = new byte[65535];
datagramPacket = new DatagramPacket(buffer,
buffer.length);
datagramSocket.receive(datagramPacket);
String received = new String(buffer).trim();
System.out.println(received);
if (received.equalsIgnoreCase("exit")) {
datagramSocket.close();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5)RSA
package labprogram_cn;
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
package labprogram_cn;
import java.util.*;
public class lb {
public static int min(int a,int b) {
if(a<b)
return a;
else
return b;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int cap,oprt,temp,cont,ch,i=0,dr=0,x,res;
int [] arr=new int[26];
System.out.println("Leaky Bucket Algo");
System.out.println("Enter bucket size");
cap=sc.nextInt();
System.out.println("Enter output rate");
oprt=sc.nextInt();
do {
System.out.println("Enter the packet entering in
second "+(i+1));
arr[i++]=sc.nextInt();
System.out.println("Enter 1 to insert packet or 0 to
exit");
ch=sc.nextInt();
}while(ch==1);
temp=i;
System.out.println("Second:Packet sent:Packet
received:In bucket :Dropped\n");
for(cont=i=0;(cont>0)||(i<temp);i++)
{
System.out.print(" :"+(i+1));
System.out.print(" \t:"+arr[i]);
res=min(cont+arr[i],oprt);
System.out.print(" \t:"+res);
if((x=cont+arr[i]-oprt)>0){
if(x>cap) {
cont=cap;
dr=x-cap;
}
else {
cont=x;
dr=0;
}
}
else
cont=0;
System.out.print(" \t\t"+cont);
System.out.print(" \t\t"+dr+"\n");
}
sc.close();
}