Professional Documents
Culture Documents
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
Output : Test 2
import java.util.Scanner;
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 :
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 :