-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.java
More file actions
240 lines (226 loc) · 5.65 KB
/
SocketClient.java
File metadata and controls
240 lines (226 loc) · 5.65 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package simpleSocket;
import java.io.*;
import java.net.*;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class SocketClient
{
String PREFIX = "cs5700spring2015";
String HELLO;
String STATUS;
String SOLUTION;
String BYE;
private static Socket client = null;
private SocketClient(int port, boolean SSL, String host, String ID)
{
HELLO = PREFIX + " " + "HELLO" + " " + ID + "\n";
/*
* System.out.println(ID); System.out.println(port);
* System.out.println(host); System.out.println(HELLO);
*/
try
{
if (!SSL)
{
client = new Socket();
}
else
{
System.setProperty("javax.net.ssl.trustStore", "./kclient.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "*****");
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
client = (SSLSocket) sslsocketfactory.createSocket();
}
SocketAddress sockAddr = new InetSocketAddress(host, port);
client.connect(sockAddr, 1000);
socketApp(client);
}
catch (IOException e)
{
System.out.println("Cannot connect to the server!");
e.printStackTrace();
}
}
private String stringToAscii(String str)
{
byte[] bytes = null;
try
{
bytes = str.getBytes("US-ASCII");
}
catch (UnsupportedEncodingException e)
{
System.out.println("HELLO message not follows ASCII standard");
e.printStackTrace();
}
if ((bytes == null) || (bytes.length == 0))
{
return "";
}
char[] ascii = new char[bytes.length];
for (int i = 0; i < bytes.length; i++)
{
ascii[i] = (char) bytes[i];
}
return new String(ascii);
}
private void socketApp(Socket client)
{
try
{
Writer writer = new OutputStreamWriter(client.getOutputStream());
writer.write(stringToAscii(HELLO));
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(
client.getInputStream()));
String message;
while ((message = reader.readLine()) != null)
{
if (isValidBye(message))
{
BYE = message;
writer.close();
reader.close();
client.close();
break;
}
else
{
STATUS = message;
String results = solveSolution(STATUS);
SOLUTION = PREFIX + " " + results + "\n";
writer.write(SOLUTION);
writer.flush();
SOLUTION = PREFIX + " ";
}
}
if (BYE == null)
{
throw new IllegalArgumentException(
"Program did not end correctly, please check hello message");
}
else
{
System.out.println(BYE);
}
}
catch (IOException e)
{
System.out.println("Cannot write to or read from the server!");
e.printStackTrace();
}
}
private String solveSolution(String STATUS)
{
if (STATUS == null)
{
throw new IllegalArgumentException("Invalid status (Can not be null)!");
}
String delims = "[ ]+";
String[] tokens = STATUS.split(delims);
if (tokens.length != 5)
{
System.out.println("STATUS: " + STATUS);
throw new IllegalArgumentException("Invalid status (Incorrect number of field)!");
}
if (!tokens[0].equals("cs5700spring2015") || !tokens[1].equals("STATUS"))
{
System.out.println("STATUS: " + STATUS);
throw new IllegalArgumentException("Invalid status (Incorrect value of field)!");
}
float operator1 = Float.valueOf(tokens[2]);
float operator2 = Float.valueOf(tokens[4]);
if ((operator1 < 1 || operator1 > 1000) || (operator2 < 1 || operator2 > 1000))
throw new IllegalArgumentException("Invalid status (Operators out of bounds [1,1000])!");
if (tokens[3].equals("+"))
return Integer.toString((int) Math.floor(operator1 + operator2));
else if (tokens[3].equals("-"))
return Integer.toString((int) Math.floor(operator1 - operator2));
else if (tokens[3].equals("*"))
return Integer.toString((int) Math.floor(operator1 * operator2));
else if (tokens[3].equals("/"))
return Integer.toString((int) Math.floor(operator1 / operator2));
else
throw new IllegalArgumentException("Invalid status (Operation not acceptable)");
}
private boolean isValidBye(String message)
{
if (message == null)
{
return false;
}
String delims = "[ ]+";
String[] tokens = message.split(delims);
if (tokens.length != 3)
return false;
if (!tokens[0].equals("cs5700spring2015") || !tokens[2].equals("BYE"))
return false;
return true;
}
private static boolean isValidPortNumber(String port)
{
for (int i = port.length(); --i >= 0;)
{
if (!Character.isDigit(port.charAt(i)))
{
return false; // if any char of string port is not digital,
// return false
}
}
// We can sure that port is at least an integer
if (Integer.parseInt(port) < 1024 || Integer.parseInt(port) > 65535)
return false;
else
return true;
}
private static int GetIndexOfArg(String[] args, String argument)
{
for (int i = 0; i < args.length; i++)
{
if (args[i].equals(argument))
return i;
}
return -1;
}
public static void main(String[] args)
{
if (args.length < 2)
throw new IllegalArgumentException("Please give me the host address and ID!");
int port = -1;
boolean SSL = false;
String ID = args[args.length - 1];
String host = args[args.length - 2];
int indexOfPort = GetIndexOfArg(args, "-p");
if (indexOfPort != -1)
{
if (!isValidPortNumber(args[indexOfPort + 1]))
throw new IllegalArgumentException("Invalid port number! (port number should be"
+ "an integer with range (1024, 65535])");
else
{
port = Integer.parseInt(args[indexOfPort + 1]);
}
}
if (GetIndexOfArg(args, "-s") != -1)
{
SSL = true;
}
else
{
SSL = false;
}
if (port == -1)
{
if (!SSL)
{
port = 27993;
}
else
{
port = 27994;
}
}
new SocketClient(port, SSL, host, ID);
}
}