-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRCReceiver.java
66 lines (51 loc) · 1.91 KB
/
CRCReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
import java.net.*;
public class Receiver {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(3310);
System.out.println("Waiting for sender...");
Socket socket = serverSocket.accept();
System.out.println("Sender connected");
InputStream inputStream = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String receivedData = in.readLine();
String divisor = in.readLine();
int errorDetected = crcCheck(receivedData, divisor);
if (errorDetected != 0) {
System.out.println("Error detected in the received data.");
} else {
System.out.println("No errors detected in the received data.");
}
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int crcCheck(String data, String divisor) {
int dataLength = data.length();
int divisorLength = divisor.length();
char[] dataChars = data.toCharArray();
char[] remainder = new char[dataLength];
System.arraycopy(dataChars, 0, remainder, 0, divisorLength - 1);
for (int i = 0; i < dataLength - divisorLength + 1; i++) {
if (remainder[i] == '1') {
xorOperation(remainder, divisor, i);
}
}
for (char c : remainder) {
if (c == '1')
return 1;
}
return 0;
}
public static void xorOperation(char[] a, String b, int startIndex) {
for (int i = 0; i < b.length(); i++) {
if (a[i + startIndex] == b.charAt(i))
a[i + startIndex] = '0';
else
a[i + startIndex] = '1';
}
}
}