CS3591 - Computer Networks Lab
CS3591 - Computer Networks Lab
CS3591 - Computer Networks Lab
SEMESTER-V
BONAFIDE CERTIFICATE
Certified that this is a record work done for CS3591- COMPUTER NETWORKS
Selvan/Selvi.……………………….………with registerNo…..........................................Studying
in the fifth semester BE (Computer Science And Engineering)in JP COLLEGE OF
ENGINEERING, during the academic year 2023-2024.
EX.NO: 1
DATE:
AIM:
Commands:
Tcpdump:
To display all traffic between two hosts (represented by variables host1 and host2):
# tcpdump srchost
# tcpdump dst
host
Display traffic for a specific protocol
Provide the protocol as an argument to display only traffic for a specific protocol, for
example tcp,udp, icmp, arp
# tcpdump protocol
# tcpdump tcp
3. ipconfig
In Windows, ipconfig is a console application designed to run from the Windows
command Prompt. This utility allows you to get the IP address information of a Windows
computer.
Using ipconfig
From the command prompt, type ipconfig to run the utility with default options. The output of
the Default command contains the IP address, network mask, and gateway for all physical and
virtual Network adapter.
#ipconfig
4. nslookup
The nslookup (which stands for name server lookup) command is a network utility
program used to obtain information about internet servers. It finds name server information for
domains by queryingthe Domain Name System.
The nslookup command is a powerful tool for diagnosing DNS problems. You know
you're experiencing a DNS problem when you can access a resource by specifying its IP
address but not itsDNS name.
#nslookup
5. Trace route:
Trace route uses Internet Control Message Protocol (ICMP) echo packets with variable
time to live(TTL) values. The response time of each hop is calculated. To guarantee accuracy,
each hop is queried multiple times (usually three times) to better measure the response of that
particular hop. Trace route is a network diagnostic tool used to track the pathway taken by a
packet on an IP network from source to destination. Trace route also records the time taken for
each hop the packet makes during its route to the destination. Trace route uses Internet Control
Message Protocol (ICMP) echo packets with variable time to live (TTL) values.
The response time of each hop is calculated. To guarantee accuracy, each hop is queried
multiple times (usually three times) to better measure the response of that particular hop.
Traceroute sends packets with TTL values that gradually increase from packet to packet, starting
with TTL value of one. Routers decrement TTL values of packets by one when routing and
discard packets whose TTL value has reached zero, returning the ICMP error message
ICMP Time Exceeded.
For the first set of packets, the first router receives the packet, decrements the TTL value
and drops the packet because it then has TTL value zero. The router sends an ICMP Time
Exceeded message back to the source. The next set of packets are given a TTL value of two, so
the first router forwards the packets, but the second router drops them and replies with ICMP
Time Exceeded. Proceeding in this way, trace route uses the returned ICMP Time Exceeded
messages to build a list of routers that packets traverse, until the destination is reached and
returns an ICMP Echo
Reply message.
With the tracert command shown above, we're asking tracert to show us the path from the
localcomputer all the way to the network device with the hostname www.google.com.
#tracert google.com
6. Ping:
The ping command sends an echo request to a host available on the network. Using this
command, you can check if your remote host is responding well or not. Tracking and isolating
hardware and software problems. Determining the status of the network and various foreign
hosts. The ping command is usually used as a simple way to verify that a computer can
communicate over the network with another computer or network device. The ping command
operates by sending Internet Control Message Protocol (ICMP) Echo Request messages to the
destination computer and waiting for a response
# ping172.16.6.2
RESULT:
Write a HTTP web client program to download a web page using TCP sockets
EX.NO: 2
DATE:
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
import java.net.*;
public class SocketHTTPClient
{
public static void main(String[] args)
{
String hostName =
"www.sunnetwork.in"; int portNumber =
80;
try
{
Socket socket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("GET / HTTP/1.1\nHost: www.sunnetwork.in\n\n");
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host " + hostName);System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to " + hostName);System.exit(1);
}
}
}
OUTPUT:
RESULT:
Applications using TCP sockets like Echo client and echo server
EX.NO:3a
DATE:
AIM:
server: ALGORITHM:
Client:
Server:
PROGRAM:
EchoServer.java
import java.net.*;
import java.io.*;
public class EServer
{
public static void main(String args[])
{
ServerSocket
s=null;
String line;
DataInputStream is;
PrintStream ps;
Socket c=null;
try
{
s=new ServerSocket(9000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
c=s.accept();
is=new
DataInputStream(c.getInputStream());
ps=new PrintStream(c.getOutputStream());
while(true)
{
line=is.readLine();
ps.println(line);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
EClient.java
import java.net.*;
import java.io.*;
public class EClient
{ public static void main(String arg[])
{
Socket c=null;
String line;
DataInputStream is,is1;
PrintStream os;
try
{
InetAddress ia =
InetAddress.getLocalHost(); c=new
Socket(ia,9000);
}
catch(IOException e)
{
System.out.println(e);
}
try
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new DataInputStream(c.getInputStream());while(true)
{
System.out.println("Client:");line=is.readLine(); os.println(line);
System.out.println("Server:" + is1.readLine());
}
}
catch(IOException e)
{
System.out.println("Socket Closed!");
}
}}
OUTPUT:
Server
Client
Applications using TCP sockets like Echo Chat
EX.NO:3b
DATE:
AIM:
b.Chat:
ALGORITHM:
Client:
Server:
PROGRAM:
UDPserver.
java import
java.io.*; import
java.net.*; class
UDPserver
{
{
ds=new DatagramSocket(clientport);
System.out.println("press ctrl+c to quit the
program");
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
InetAddress ia=InetAddress.geyLocalHost();
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());
System.out.println("Client:" + psx); System.out.println("Server:");
String str=dis.readLine();
if(str.equals("end")) break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,serverport));
}
UDPclient.java
import java .io.*;
import java.net.*;
class UDPclient
{
public static DatagramSocket ds;
public static int clientport=789,serverport=790;
public static void main(String args[])throws Exception
{
byte buffer[]=new byte[1024]; ds=new DatagramSocket(serverport);
BufferedReader dis=new BufferedReader(new InputStreamReader(System.in));
System.out.println("server waiting");
InetAddress ia=InetAddress.getLocalHost();
while(true)
{
System.out.println("Client:");
String str=dis.readLine();
if(str.equals("end"))
break;
buffer=str.getBytes();
ds.send(new DatagramPacket(buffer,str.length(),ia,clientport));
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
String psx=new String(p.getData(),0,p.getLength());
System.out.println("Server:" + psx);
}
}
}
OUTPUT:
RESULT:
Simulation of DNS using UDP sockets.
EX.NO: 4
DATE:
Aim :
Algorithm:
PROGRAM:
UDP DNS Server
import java.io.*;
import java.net.*;
public class
dnsserver
{
private static int indexOf(String[] array, String str)
{
str = str.trim();
for (int i=0; i < array.length; i++)
{
if (array[i].equals(str)) return i;
}
return -1;
}
public static void main(String arg[])throws IOException
{
String[] hosts = {"zoho.com", "gmail.com","google.com", "facebook.com"};
String[] ip = {"172.28.251.59",
"172.217.11.5","172.217.11.14","31.13.71.36"};
System.out.println("Press Ctrl + C to Quit");while (true)
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket recvpack = new
DatagramPacket(receivedata,receivedata.length); serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress =
recvpack.getAddress(); int port =
recvpack.getPort();
String capsent;
System.out.println("Request for host " +
sen); if(indexOf (hosts, sen) != -1)
capsent = ip[indexOf (hosts,
sen)]; else
capsent = "Host Not Found";
senddata =
capsent.getBytes();
DatagramPacket pack = new DatagramPacket (senddata, senddata.length,ipaddress,port);
serversocket.send(pack);
serversocket.close();
}
}
}
//UDP DNS Client:
import java.io.*;
import java.net.*;
public class dnsclient
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new
byte[1024]; int portaddr = 1362;
System.out.print("Enter the hostname : ");
String sentence = br.readLine();
senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,senddata.length,ipaddress,portaddr);
clientsocket.send(pack);
DatagramPacket recvpack =new DatagramPacket(receivedata,receivedata.length);
clientsocket.receive(recvpack);
String modified = new
String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}
}
OUTPUT
Server
Client
RESULT:
Use a tool like Wireshark to capture packets and examine the packets
EX.NO:5
DATE:
AIM:
Wireshark:
Wireshark is an open-source packet analyzer, which is used for education, analysis, software
development, communication protocol development, and network troubleshooting.
It is used to track the packets so that each one is filtered to meet our specific needs. It is
commonly called as a sniffer, network protocol analyzer, and network analyzer. It is also used by network
security engineers to examine security problems.
Wireshark is a free to use application which is used to apprehend the data back and forth. It is
often called as a free packet sniffer computer application. It puts the network card into an unselective
mode, i.e., to accept all the packets which it receives.
Uses of Wireshark:
1. It is used by network security engineers to examine security problems.
2. It allows the users to watch all the traffic being passed over the network.
3. It is used by network engineers to troubleshoot network issues.
4. It also helps to troubleshoot latency issues and malicious activities on your network.
5. It can also analyze dropped packets.
6. It helps us to know how all the devices like laptop, mobile phones, desktop, switch, routers, etc.,
communicate in a local network or the rest of the world.
PACKET:
A packet is a unit of data which is transmitted over a network between the origin and the
destination. Network packets are small, i.e., maximum 1.5 Kilobytes for Ethernet packets and 64
Kilobytes for IP packets. The data packets in the Wireshark can be viewed online and can be analyzed
offline.
Functionality of Wireshark:
Wireshark is similar to tcpdump in networking. Tcpdump is a common packet analyzer which
allows the user to display other packets and TCP/IP packets, being transmitted and received over a
network attached to the computer. It has a graphic end and some sorting and filtering functions.
Wireshark users can see all the traffic passing through the network.
Wireshark can also monitor the unicast traffic which is not sent to the network's MAC address
interface. But, the switch does not pass all the traffic to the port. Hence, the promiscuous mode is not
sufficient to see all the traffic. The various network taps or port mirroring is used to extend capture at any
point.
Port mirroring is a method to monitor network traffic. When it is enabled, the switch sends the
copies of all the network packets present at one port to another port.
What is color coding in Wireshark?
The packets in the Wireshark are highlighted with blue, black, and green color. These colors help users to
identify the types of traffic. It is also called as packet colorization. The kinds of coloring rules in the
Wireshark are temporary rules and permanent rules. The temporary rules are there until the program is in
active mode or until we quit the program.
PROCEDURE:
1. Open theWireshark application.
2. Select the current interface. Eg. WiFi
RESULT:
Write a code simulating ARP /RARP protocols
EX.NO:6
DATE:
AIM:
ALGORITHM:
Client
Server
PROGRAM
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp
{
public static void main(String args[])
{
Try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket clsct=new Socket("127.0.0.1",139)
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
System.out.println("Enter the Logical
address(IP):"); String str1=in.readLine();
dout.writeBytes(str1+'\n');
String str=din.readLine();
System.out.println("The Physical Address is: "+str);
clsct.close();
}
catch (Exception e)
{
System.out.println(e);
}}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverarp
{
public static void main(String args[])
{
try{
ServerSocket obj=new
ServerSocket(139);
Socketobj1=obj.accept();
while(true)
{
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream dout=new
DataOutputStream(obj1.getOutputStream()); String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(str.equals(ip[i]))
{
dout.writeBytes(mac[i]+'\n');break;
}
}
obj.close();
}
}
catch(Exception e)
{
System.out.println(e);
}}}
OUTPUT:
(b) Program for Reverse Address Resolution Protocol (RARP) using UDP
ALGORITHM:
Client:
Server:
PROGRAM:
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp12
{
public static void main(String args[])
{
Try
{
DatagramSocket client=new DatagramSocket();
InetAddress addr=InetAddress.getByName("127.0.0.1");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Physical address (MAC):")
String str=in.readLine();
sendbyte=str.getBytes();
DatagramPacket sender=newDatagramPacket(sendbyte,sendbyte.length,addr,1309);
client.send(sender);
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
client.receive(receiver);
String s=new String(receiver.getData());
System.out.println("The Logical Address is(IP): "+s.trim());client.close();
}
catch(Exception e)
{
System.out.println(e);
}}}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverrarp12
{
public static void main(String args[])
{
Try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new
byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new
String(receiver.getData()); String
s=str.trim();
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
String ip[]={"165.165.80.80","165.165.79.1"};
String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
for(int i=0;i<ip.length;i++)
{
if(s.equals(mac[i]))
{
sendbyte=ip[i].getBytes();
DatagramPacket sender = new DatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
break;}}
break;}}}catch(Exception e)
{
System.out.println(e);}}}
OUTPUT:
RESULT:
Study of Network simulator (NS) & Simulation of Congestion Control Algorithms using NS
EX.NO:7
DATE:
AIM:
Theory:
NET WORK SIMULATOR (NS2)
Ns Overview
Ns Status
Periodical release (ns-2.26, Feb2003)Platform support
FreeBSD, Linux, Solaris, Windows and Mac
Ns functionalities
Slow start
Additive increase/multiplicative decrease
Fast retransmit and Fast recovery
served. Thus queue length is very important characteristic to determine that how well the active
queue management of the congestion control algorithm has been working.
Congestion control Algorithms
Slow-start is used in conjunction with other algorithms to avoid sending more data than the
network is capable of transmitting, that is, to avoid causing network congestion. The additive
increase/multiplicative decrease (AIMD) algorithm is a feedback control algorithm. AIMD
combines linear growth of the congestion window with an exponential reduction when a
congestion takes place. Multiple flows using AIMD congestion control will eventually converge
to use equal amounts of a contended link. Fast Retransmit is an enhancement to TCP that
reducesthe time a sender waits before retransmitting a lost segment.
PROGRAM:
include <wifi_lte/wifi_lte_rtable.h>
struct r_hist_entry *elm, *elm2;
int num_later = 1;
elm = STAILQ_FIRST(&r_hist_);
while (elm != NULL && num_later <= num_dup_acks_)
{
num_later;
elm = STAILQ_NEXT(elm, linfo_);
}
if (elm != NULL)
{
elm = findDataPacketInRecvHistory(STAILQ_NEXT(elm,linfo_));
if (elm != NULL)
{
elm2 = STAILQ_NEXT(elm,
linfo_); while(elm2 != NULL)
{
if (elm2->seq_num_ < seq_num && elm2->t_recv_ <time)
{ STAILQ_REMOVE(&r_hist_,elm2,r_hist_entry,linfo_);
delete elm2;
} else
elm = elm2;
elm2 = STAILQ_NEXT(elm, linfo_);
}
}
}
}
void DCCPTFRCAgent::removeAcksRecvHistory()
{ struct r_hist_entry *elm1 = STAILQ_FIRST(&r_hist_);
struct r_hist_entry *elm2;
int num_later = 1;
while (elm1 != NULL && num_later <= num_dup_acks_)
{
num_later;
elm1 = STAILQ_NEXT(elm1, linfo_);
}
if(elm1 ==
NULL) return;
elm2 = STAILQ_NEXT(elm1,
linfo_); while(elm2 != NULL){
if (elm2->type_ == DCCP_ACK)
{ STAILQ_REMOVE(&r_hist_,elm2,r_hist_entry,linfo_);
delete elm2;
}
else
{
elm1 = elm2;
}
elm2 = STAILQ_NEXT(elm1, linfo_);
}
}
inline r_hist_entry *DCCPTFRCAgent::findDataPacketInRecvHistory(r_hist_entry
*start)
{
while(start != NULL && start->type_ ==
DCCP_ACK) start =
STAILQ_NEXT(start,linfo_);
return start;
}
RESULT:
Study of TCP/UDP performance using Simulation tool
EX.NO:8
DATE:
AIM:
TCP Performance:
Algorithm:
PROGRAM:
RESULT:
Simulation of Distance Vector/ Link State Routing algorithm
EX.NO: 9
DATE:
AIM:
ALGORITHM:
LINK STATE ROUTING
PROTOCOL PROGRAM
set ns [new Simulator]
$ns rtproto LS
set nf [open linkstate.nam w]
$ns namtrace-all $nf
set f0 [open linkstate.tr w]
$ns trace-all $f0proc finish {} { global ns f0 nf
$ns flush-traceclose $f0
close $nf
exec nam linkstate.nam &exit 0
}
for {set i 0} {$i <7} {incr i} {set n($i) [$ns node]
}
for {set i 0} {$i <7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop”
$ns at 5.0 "finish"
$ns run
OUTPUT:
DISTANCE VECTOR ROUTING
ALGORITHM ALGORITHM:
PROGRAM:
#Distance vector routing protocol – distvect.tcl#Create a simulator object
set ns [new Simulator]
#Use distance vector routing
$ns rtproto DV
#Open the nam trace fileset nf [open out.nam w]
$ns namtrace-all $nf# Open tracefile
set nt [open trace.tr w]
$ns trace-all $nt
#Define 'finish' procedureproc finish {}
{ global ns nf
$ns flush
-trace
#Close the trace fileclose $nf
#Execute nam on the trace fileexec nam
-a out.nam &exit 0
}
# Create 8 nodesset n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4
[$ns node] set n5 [$ns node] set n6 [$ns node] set n7 [$ns node] set n8 [$ns
node]
# Specify link characterestics
$ns duplex
-link $n1 $n2 1Mb 10ms DropTail
$ns duplex
-link $n2 $n3 1Mb 10ms DropTail
$ns duplex
-link $n3 $n4 1Mb 10ms DropTail
$ns duplex
-link
-op $n6 $n7 orient down
$ns duplex
-link
-op $n7 $n8 orient left
-down
$ns duplex
-link
-op $n8 $n1 orient left
#Create a UDP agent and attach it to node n1set udp0 [new Agent/UDP]
$ns attach
-agent $n1 $udp0
#Create a CBR traffic source and attach it to udp0set cbr0
[new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n4set null0
[new Agent/Null]
$ns attach-agent $n4 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent and the network dynamics
$ns at 0.0 "$n1 label Source"
$ns at 0.0 "$n4 label Destination"
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n3 $n4
$ns duplex
-link $n4 $n5 1Mb 10ms DropTail
$ns duplex
-link $n5 $n6 1Mb 10ms DropTail
$ns duplex
-link $n6 $n7 1Mb 10ms DropTail
$ns duplex
-link $n7 $n8 1Mb 10ms DropTail
$ns duplex
-link $n8 $n1 1Mb 10ms DropTail# specify layout as a octagon
$ns duplex
-link
-op $n1 $n2 orient left
-up
$ns duplex
-link
-op $n2 $n3 orient up
$ns duplex
-link
-op $n3 $n4 orient right
-up
$ns duplex
-link
-op $n4 $n5 orient right
$ns duplex
-link
-op $n5 $n6 orient right
-down
$ns rtmodel-at 2.0 up $n3 $n4
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish" #Run the simulation
$ns run
OUTPUT:
RESULT:
Simulation of Error Correction Code (like CRC)
EX.NO:10
DATE:
AIM:
ALGORITHM:
PROGRAM:
import java.io.*;
class crc_gen
{
public static void main(String args[]) throws IOException {
BufferedReader br=new BufferedReader(newInputStreamReader(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=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
System.out.print("Data bits are : ");
for(int i=0; i< data_bits; i++)
System.out.print(data[i]);
System.out.println();
System.out.print("divisor bits are : ");
for(int i=0; i< divisor_bits; i++)
System.out.print(divisor[i]);
System.out.println();
*/ 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
remainder
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]=Integer.parseInt(br.readLine());
System.out.print("crc bits are :
"); for(int i=0; i< crc.length; i++)
System.out.print(crc[i]);
System.out.println();
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 :
RESULT: