-
Notifications
You must be signed in to change notification settings - Fork 165
/
checksum_sender.java
94 lines (61 loc) · 2.39 KB
/
checksum_sender.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package checksum_sender;
import java.io.*;
import java.net.*;
import java.util.*;
public class Checksum_Sender
{
private int MAX = 100;
private Socket socket = null;
private ServerSocket servsock = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
public Checksum_Sender(int port) throws IOException
{
servsock = new ServerSocket(port);
socket = servsock.accept();
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
while (true)
{
int i, l, sum = 0, nob;
Scanner sc = new Scanner(System.in);
System.out.println("Enter data length");
l = sc.nextInt();
int data[] = new int[MAX];
int c_data[] = new int[MAX];
System.out.println("Enter data to send");
for (i = 0; i < l; i++)
{
data[i] = sc.nextInt();
nob = (int)(Math.floor(Math.log(data[i]) / Math.log(2))) + 1;
c_data[i] = ((1 << nob) - 1) ^ data[i];
sum += c_data[i];
}
data[i] = sum;
l += 1;
System.out.println("Checksum Calculated is : " + sum);
System.out.println("Data being sent along with Checksum.....");
dos.writeInt(l);
for (int j = 0; j < l; j++)
dos.writeInt(data[j]);
if (dis.readUTF().equals("success"))
{
System.out.println("Thanks for the feedback!! Message received
Successfully!");
break;
}
else if (dis.readUTF().equals("failure"))
{
System.out.println("Message was not received successfully!");
break;
}
}
dis.close();
dos.close();
socket.close();
}
public static void main(String args[]) throws IOException
{
Checksum_Sender cs = new Checksum_Sender(45678);
}
}