-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
193 lines (170 loc) · 8.96 KB
/
Copy pathServer.java
File metadata and controls
193 lines (170 loc) · 8.96 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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
// Error codes
private static int incorrectOperationCommand = -1;
private static int numInputsLTTwo = -2;
private static int numInputsGTFour = -3;
private static int nan = -4;
private static int exitCode = -5;
// Acceptable operations
public enum Operation {
add, subtract, multiply
}
// Helper method to print in the "get: " format
// parameters:
// message - String to print with "get: " appended
public static void printGet(String message) {
System.out.print("get: " + message);
}
// Helper method to print in the ", return: " format
// parameters:
// message - String to print with ", return: " appended
public static void printReturn(String message) {
System.out.println(", return: " + message);
}
public static void main(String[] args) throws Exception {
if(args.length != 1){
System.out.println("Not enough arguments");
System.exit(-1);
}
ServerSocket server = null;
Socket client = null;
BufferedReader inBuffer = null;
PrintWriter outWriter = null;
String message = null;
// Get port number from args
int serverPort = Integer.parseInt(args[0]);
try {
// Initiate ServerSocket
server = new ServerSocket(serverPort);
try {
// Loop that waits for a client
while(true) {
try {
// Establish connection with client
client = server.accept();
System.out.println("get connection from ... " + client.getInetAddress().getHostAddress());
try {
inBuffer = new BufferedReader(new InputStreamReader(client.getInputStream()));
outWriter = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.out.println("Could not create BufferedReader and/or PrintWriter");
client.close();
System.exit(-1);
}
// Send "Hello!" to client
outWriter.println("Hello!");
// Loop that constantly reads from client
while(true) {
message = inBuffer.readLine();
if(message != null) {
printGet(message);
} else {
System.out.println("Client closed unexpectedly");
client.close();
}
String[] messageArray = message.split("\\s+");
if (message.equalsIgnoreCase("bye")) {
outWriter.println(exitCode);
printReturn(Integer.toString(exitCode));
// Close current client socket connection
client.close();
}
else if(message.equalsIgnoreCase("terminate")) {
outWriter.println(exitCode);
printReturn(Integer.toString(exitCode));
// Close current client and server sockets and terminate
client.close();
server.close();
System.exit(0);
}
else {
boolean noErrors = true;
// Check if add, subtract, or multiply
try {
Operation operation = Operation.valueOf(messageArray[0]);
// Check number of inputs
if(messageArray.length < 3) {
// Send error code -2 for number of inputs is less than two
outWriter.println(numInputsLTTwo);
printReturn(Integer.toString(numInputsLTTwo));
}
else if(messageArray.length > 5) {
// Send error code -3 for number of inputs is more than 4
outWriter.println(numInputsGTFour);
printReturn(Integer.toString(numInputsGTFour));
}
else {
// Check everything is an int
for(int i = 1; i < messageArray.length; i++) {
if(!messageArray[i].matches("\\d+")) {
// Send error code -4 for inputs contains non-numbers
outWriter.println(nan);
printReturn(Integer.toString(nan));
noErrors = false;
break;
}
}
if(noErrors) {
switch (operation) {
case add:
int sum = 0;
for(int i = 1; i < messageArray.length; i++) {
sum += Integer.parseInt(messageArray[i]);
}
outWriter.println(sum);
printReturn(Integer.toString(sum));
break;
case subtract:
int sub = Integer.parseInt(messageArray[1]);
for(int i = 2; i < messageArray.length; i++) {
sub -= Integer.parseInt(messageArray[i]);
}
outWriter.println(sub);
printReturn(Integer.toString(sub));
break;
case multiply:
int mult = Integer.parseInt(messageArray[1]);
for(int i = 2; i < messageArray.length; i++) {
mult *= Integer.parseInt(messageArray[i]);
}
outWriter.println(mult);
printReturn(Integer.toString(mult));
break;
default:
// Send error code -1 for incorrect operation command
outWriter.println(incorrectOperationCommand);
printReturn(Integer.toString(incorrectOperationCommand));
break;
}
}
}
} catch(IllegalArgumentException e) {
// Send error code -1 for incorrect operation command
outWriter.println(incorrectOperationCommand);
printReturn(Integer.toString(incorrectOperationCommand));
}
}
}
} catch(Exception e) {
} finally {
// Ensure client is closed
client.close();
}
}
} finally {
// Ensure server is closed
server.close();
}
}
catch(IOException e) {
System.out.println("ServerSocket could not be made");
System.exit(-1);
}
}
}