-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotebankudp.java
118 lines (117 loc) · 4.46 KB
/
remotebankudp.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.net.*;
import java.security.*;
public class remotebankudp {
static int maxtry=3;
public static void main(String args[]) throws Exception {
try{
if(args.length!=5 && args.length!=6){
throw new IllegalArgumentException("your input is invalid");
}
String[] temp = args[0].split(":");
InetAddress destAddr = InetAddress.getByName("127.0.0.1"); // Destination address
int portnum = 1234;
try {
destAddr = InetAddress.getByName(temp[0]);
portnum = Integer.parseInt(temp[1]);
if(portnum>65535 ||portnum<1024){
portnum = 1234;
System.out.println("Invalid input. Set portnum to 1234");
}
}
catch (Exception e){
System.out.println("Invalid input. Set address to local, set portnum to 1234");
}
DatagramSocket clientSocket = new DatagramSocket();
byte[] outputdata = new byte[1024];
Boolean debug = false;
if(args.length == 6){
if(args[5].equals("-d")){
debug = true;
}
else{
System.out.println("Unregonized command. Use -d to debug");
}
}
String authentication = "request authentication,";
if(debug == true){
authentication = "request authentication,-d,";
}
outputdata = authentication.getBytes();
DatagramPacket sendPacket = new DatagramPacket(outputdata, outputdata.length, destAddr, portnum);
clientSocket.send(sendPacket);
if(debug == true){
System.out.println("Sending authentication request to server");
}
clientSocket.setSoTimeout(2000); // set the timeout in millisecounds.
while(maxtry >0){// recieve data until timeout
try {
byte[] inputdata = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(inputdata, inputdata.length);
clientSocket.receive(receivePacket);
byte[]buffer = receivePacket.getData();
byte[] bufferData = new byte[64];
for(int m = 0; m<bufferData.length; m++){
bufferData[m] = buffer[m];
}
String modifiedSentence = new String(bufferData);
String sentence = args[1]+",";
String hashString = args[1]+args[2]+modifiedSentence;
byte[] bytesOfMessage = hashString.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < thedigest.length; ++i) {
sb.append(Integer.toHexString((thedigest[i] & 0xFF) | 0x100).substring(1,3));
}
sentence+= sb.toString()+",";
for(int i =3;i<args.length;i++){
sentence+=args[i]+",";
}
try{
Double balance= Double.parseDouble(args[4]);
}
catch(Exception e){
throw new IllegalArgumentException("invalid input");
}
if(debug == true){
sentence+="-d,";
}
outputdata = sentence.getBytes();
sendPacket = new DatagramPacket(outputdata, sentence.getBytes().length, destAddr, portnum);
clientSocket.send(sendPacket);
if(debug == true){
System.out.println("Sending Hash String to server");
}
clientSocket.setSoTimeout(2000); // set the timeout in millisecounds.
while(maxtry >0){
try {
byte[] inputdataNew = new byte[1024];
receivePacket = new DatagramPacket(inputdataNew, inputdataNew.length);
clientSocket.receive(receivePacket);
modifiedSentence = new String(receivePacket.getData());
System.out.println(modifiedSentence);
clientSocket.close();
}
catch (SocketTimeoutException e) {
System.out.println("Timeout reached " + e);
if(debug == true){
System.out.println("Resending...");
}
maxtry--;
}
}
}
catch (SocketTimeoutException e) {
System.out.println("Timeout reached " + e);
if(debug == true){
System.out.println("Resending...");
}
maxtry--;
}
}
clientSocket.close();
}
catch (SocketException e1) {
}
}
}