0% found this document useful (0 votes)
37 views6 pages

Program 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Program 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Network Laboratory [BCS502] 2024-25

Java is a general-purpose computer programming language that is simple, concurrent, class-


based, object-oriented language. The compiled Java code can run on all platforms that support Java
without the need for recompilation hence Java is called as "write once, run anywhere" (WORA).
The Java compiled intermediate output called “byte-code” that can run on any Java virtual machine
(JVM) regardless of computer architecture. The language derives much of its syntax from C and C++,
but it has fewer low-level facilities than either of them.
In Linux operating system Java libraries are preinstalled. It’s very easy and convenient to compile and
run Java programs in Linux environment. To compile and run Java Program is a two-step process:
1. Compile Java Program from Command Prompt
[root@host ~]# javac Filename.java

The Java compiler (Javac) compiles java program and generates a byte-code with the same file
name and .class extension.

2. Run Java program from Command Prompt


[root@host ~]# java Filename

The java interpreter (Java) runs the byte-code and gives the respective output. It is important to note
that in above command we have omitted the .class suffix of the byte-code (Filename.class).

Page 1
Computer Network Laboratory [BCS502] 2024-25

Program- 4
Write a program for error detecting code using CRC-CCITT (16- bits).
Whenever digital data is stored or interfaced, data corruption might occur. Since the beginning
of computer science, developers have been thinking of ways to deal with this type of problem.
For serial data they came up with the solution to attach a parity bit to each sent byte. This
simple detection mechanism works if an odd number of bits in a byte changes, but an even
number of false bits in one byte will not be detected by the parity check. To overcome this
problem developers have searched for mathematical sound mechanisms to detect multiple false
bits. The CRC calculation or cyclic redundancy check was the result of this. Nowadays CRC
calculations are used in all types of communications. All packets sent over a network
connection are checked with a CRC. Also each data block on your hard disk has a CRC value
attached to it. Modern computer world cannot do without these CRC calculations. So let's see
why they are so widely used. The answer is simple; they are powerful, detect many types of
errors and are extremely fast to calculate especially when dedicated hardware chips are used.
The idea behind CRC calculation is to look at the data as one large binary number. This
number is divided by a certain value and the remainder of the calculation is called the CRC.
Dividing in the CRC calculation at first looks to cost a lot of computing power, but it can be
performed very quickly if we use a method similar to the one learned at school. We will as an
example calculate the remainder for the character 'm'—which is 1101101 in binary notation—
by dividing it by 19 or 10011. Please note that 19 is an odd number. This is necessary as we
will see further on. Please refer to your schoolbooks as the binary calculation method here is
not very different from the decimal method you learned when you were young. It might only
look a little bit strange. Also notations differ between countries, but the method is similar.

With decimal calculations you can quickly check that 109 divided by 19 gives a quotient of 5
with 14 as the remainder. But what we also see in the scheme is that every bit extra to check

Page 2
Computer Network Laboratory [BCS502] 2024-25

only costs one binary comparison and in 50% of the cases one binary subtraction.
You can easily increase the number of bits of the test data string—for example to 56 bits if we
use our example value "Lammert"—and the result can be calculated with 56 binary
comparisons and an average of 28 binary subtractions. This can be implemented in hardware
directly with only very few transistors involved. Also software algorithms can be very efficient.
All of the CRC formulas you will encounter are simply checksum algorithms based on modulo-
2 binary division where we ignore carry bits and in effect the subtraction will be equal to an
exclusive or operation. Though some differences exist in the specifics across different CRC
formulas, the basic mathematical process is always the same:
• The message bits are appended with c zero bits; this augmented message is the dividend
• A predetermined c+1-bit binary sequence, called the generator polynomial, is the divisor
• The checksum is the c-bit remainder that results from the division operation.
Table 1 lists some of the most commonly used generator polynomials for 16- and 32-bit CRCs.
Remember that the width of the divisor is always one bit wider than the remainder. So, for
example, you’d use a 17-bit generator polynomial whenever a 16-bit checksum is required.

Page 3
Computer Network Laboratory [BCS502] 2024-25

Source code:
import java.util.*;
class crc
{
void div(int a[],int k)
{
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int count=0;
for(int i=0;i<k;i++)
{
if(a[i]==gp[0])
if(a[i]==gp[0])
{
for(int j=i;j<17+i;j++)
{
a[j]=a[j]^gp[count++];
}
count=0;
}
}
}
public static void main(String args[])
{
int a[]=new int[100];
int b[]=new int[100];
int len,k;
crc ob=new crc();
System.out.println("Enter the length of Data Frame:");
Scanner sc=new Scanner(System.in);
len=sc.nextInt();
int flag=0;
System.out.println("Enter the Message:");
for(int i=0;i<len;i++)
{ a[i]=sc.nextInt();
}
for(int i=0;i<16;i++)
{ a[len++]=0;
}
k=len-16;
for(int i=0;i<len;i++)
{ b[i]=a[i];
}
ob.div(a,k);
for(int i=0;i<len;i++)
a[i]=a[i]^b[i];
System.out.println("Data to be transmitted: ");
for(int i=0;i<len;i++)
{

Page 4
Computer Network Laboratory [BCS502] 2024-25

System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter
the Received Data: ");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
ob.div(a, k);
for(int i=0;i<len;i++)
{
if(a[i]!=0)
{
f
l
a
g
=
1
;

b
r
e
a
k
;
}
}
if(flag==1)
System.out.p
rintln("ERR
OR in data");
else
System.out.println("NO ERROR");
}
}

OUTPUT-1

Enter
the
length
of Data
Frame:
4 Enter
the

Page 5
Computer Network Laboratory [BCS502] 2024-25

Messag
e: 1 0 1
1
Data to be transmitted: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the Received Data: 1 0 1 1 1 0
1 1 0 0 0 0 0 1 1 0 1 0 1 1 ERROR in
Data

OUTPUT-2

Enter
the
length
of Data
Frame:
4 Enter
the
Messag
e: 1 0 1
1
Data to be transmitted: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the Received Data: 1 0 1 1 1 0
1 1 0 0 0 1 0 1 1 0 1 0 1 1 NO
ERROR

Page 6

You might also like