-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFEclient.java
More file actions
417 lines (347 loc) · 15.4 KB
/
Copy pathFEclient.java
File metadata and controls
417 lines (347 loc) · 15.4 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FEclient{
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
public static boolean connection = false;
public static final String CLIENT_PATH = "CSNETWK MP\\CSNETWK MP\\clientFiles";
public static String username = null;
static boolean joined = false;
static boolean bastin = false;
private void leaveServer() {
try {
// Notify the server that the client is leaving
out.writeUTF("/leave");
// Close the connection
socket.close();
in.close();
out.close();
if (bastin == true) {
try {
deleteContents(username);
bastin = false;
username = null;
} catch (IOException e) {
System.err.println("An error occurred while reading the file: " + e.getMessage());
}
System.out.println("Disconnected from the server.");
connection = false;
}
} catch (IOException e) {
System.out.println("Error: An error occurred while leaving the server - ");
}
}
public static void main (String [] args){
int check = 1; //used for knowing there is a username already
boolean end = true;
System.out.print("To begin, please enter a command. Enter /? for the list of commands: ");
Scanner sc = new Scanner(System.in);
String command = sc.nextLine();
FEclient client = new FEclient();
while(end) {
if (command.equals("/?")) {
System.out.println("Available Commands:\n");
System.out.println("-----------------------------------------------------------------------------------");
System.out.println("/join <server_ip_add> <port> |Connect to the server application");
System.out.println("/leave |Disconnect to the server application");
System.out.println("/register <handle> |Register a unique handle or alias");
System.out.println("/store <filename> |Send file to server");
System.out.println("/dir |Request directory file list from a server");
System.out.println("/get <filename> |Fetch a file from a server");
System.out.println("/? |Get command help for all input syntax references.");
System.out.println("/end |End the Application");
System.out.println("-----------------------------------------------------------------------------------");
} else if (command.startsWith("/join ")) {
String[] commandParts = command.split("\\s+");
String ip_add = commandParts[1];
int port = Integer.parseInt(commandParts[2]);
client = new FEclient(ip_add, port);
} else if (command.equals("/leave")) {
if (connection == true) {
client.leaveServer();
connection = false;
username = null;
check = 1;
System.out.println("Connection closed. Thank you!");
} else {
System.out.println("Error: Disconnection failed. Please connect to the server first.");
}
} else if (command.startsWith("/register ")) {
username = command.substring("/register ".length());
try {
if (joined == false ) {
System.out.println("Error: Not connected to Server - join first");
} else {
boolean found = checkFile(username);
if (found) {
if (check == 1 && joined == false) {
System.out.println("Error: Registration failed. Handle or alias already exists.");
} else {
System.out.println("Error: Registration failed. Handle or alias already exists.");
}
} else{
if (check == 0) {
System.out.println("Error: Registration failed. Handle or alias already exists.");
}
else if (joined == true){
System.out.println("Welcome " + username + "!");
check = 0;
bastin = true;
addContent(username);
}
}
}
} catch (IOException e) {
System.err.println("An error occurred while reading the file: " + e.getMessage());
}
} else if (command.startsWith("/store ")) {
if (bastin == true) {
String filePath = command.substring("/store ".length());
client.storeFile(filePath);
} else {
System.out.println("Error: Register first!");
}
} else if (command.startsWith("/dir")) {
if (connection == true && bastin == true) {
client.getDir();
} else {
System.out.println("Error: Directory failed. Please connect to the server first.");
}
} else if (command.startsWith("/get ")) {
if (bastin == true) {
String fileName = command.substring("/get ".length());
client.getFile(fileName);
} else {
System.out.println("Error: Register first");
}
}else if (command.startsWith("/end")) {
end = true;
break;
}else {
System.out.println("Error: Command not found.");
System.out.print("To begin, please enter a command. Enter /? for the list of commands: ");
command = sc.nextLine();
}
System.out.print("To begin, please enter a command. Enter /? for the list of commands: ");
command = sc.nextLine();
}
try {
deleteContents(username);
} catch (IOException e) {
System.err.println("An error occurred while reading the file: " + e.getMessage());
}
sc.close();
}
private boolean isConnected() {
return connection;
}
public void storeFile(String fileName) {
try {
if (!isConnected()) {
System.out.println("Error: Not connected to the server. Please connect first.");
return;
}
String filePath = CLIENT_PATH + "/" + fileName;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
System.out.println("Error: The specified file does not exist.");
return;
}
// Notify the server that the client wants to store a file
out.writeUTF("/store");
// Send the file name to the server
out.writeUTF(fileName);
// Read the file content into a byte array
byte[] fileBytes = Files.readAllBytes(file.toPath());
// Send the file size to the server
out.writeLong(fileBytes.length);
// Send the file content to the server
out.write(fileBytes);
out.flush();
System.out.println("File '" + fileName + "' sent to the server successfully.");
socket.close();
in.close();
out.close();
socket = new Socket("127.0.0.1", 12345);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.err.println("Error: An error occurred while sending the file - " + e.getMessage());
}
}
public void getFile(String fileName) {
try {
if (!isConnected()) {
System.out.println("Error: Not connected to the server. Please connect first.");
return;
}
// Check if the file exists
String filePath = FEserver.FILES_PATH + "/" + fileName;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
System.out.println("Error: The specified file does not exist.");
return;
}
// Notify the server that the client wants to store a file
out.writeUTF("/get");
// Send the file name to the server
out.writeUTF(fileName);
// Read the file content into a byte array
byte[] fileBytes = Files.readAllBytes(file.toPath());
// Send the file size to the server
out.writeLong(fileBytes.length);
// Send the file content to the server
out.write(fileBytes);
out.flush();
System.out.println("File '" + fileName + "' sent to the client successfully.");
socket.close();
in.close();
out.close();
socket = new Socket("127.0.0.1", 12345);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.err.println("Error: An error occurred while sending the file - " + e.getMessage());
}
}
public FEclient(){
}
public FEclient(String ip_add, int port) {
if (!connection){
try {
if (port < 0 || port > 65535) {
System.err.println("Error: Invalid port number. Please use a port between 0 and 65535.");
return;
}
if (!ip_add.equals("127.0.0.1")) {
System.err.println("Error: Incorrect IP.");
return;
}
socket = new Socket(ip_add, port);
System.out.println("Connection to the File Exchange Server is successful!");
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
connection = true;
joined = true;
} catch (UnknownHostException e) {
System.err.println("Error: Connection to the Server has failed! Please check IP Address and Port Number.");
} catch (IOException e) {
if (e instanceof java.net.ConnectException && e.getMessage().contains("Connection refused")) {
System.err.println("Error: Connection refused. Please check if the server is running and the port is correct.");
} else {
System.err.println("Error: Connection to the Server has failed! Please check IP Address and Port Number.");
e.printStackTrace();
}
}
}else{
System.err.println("Error: You are already connected to a server");
}
}
private static boolean checkFile(String searchText) throws IOException {
Path path = Paths.get("CSNETWK MP\\CSNETWK MP\\userList.txt");
List<String> lines = Files.readAllLines(path);
boolean found = lines.stream().anyMatch(line -> line.contains(searchText));
if (!found) {
lines.add(searchText);
}
return found;
}
private static void addContent(String searchText) throws IOException {
Path path = Paths.get("CSNETWK MP\\CSNETWK MP\\userList.txt");
List<String> lines = Files.readAllLines(path);
lines.add(searchText);
Files.write(path, lines, StandardOpenOption.WRITE);
}
private static boolean deleteContents(String searchText) throws IOException {
Path path = Paths.get("CSNETWK MP\\CSNETWK MP\\userList.txt");
// Read all lines from the file
List<String> lines = Files.readAllLines(path);
// Identify the index of the line to be deleted
int indexToRemove = -1;
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).contains(searchText)) {
indexToRemove = i;
break;
}
}
// If the line is found, remove it and write the updated lines back to the file
if (indexToRemove != -1) {
lines.remove(indexToRemove);
Files.write(path, lines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
return true;
}
return false;
}
// public void getDir() {
// try {
// if (!isConnected()) {
// System.out.println("Error: Not connected to the server. Please connect first.");
// return;
// }
// // Notify the server that the client wants to store a file
// out.writeUTF("/dir");
// String message = in.readUTF();
// System.out.println(message);
// } catch (IOException e) {
// System.err.println("Error: An error occurred while accesing the directory - " + e.getMessage());
// }
public void getDir() {
try {
if (!isConnected()) {
System.out.println("Error: Not connected to the server. Please connect first.");
return;
}
// Notify the server that the client wants to get the directory information
out.writeUTF("/dir");
// Receive the file names from the server
List<String> fileNames = receiveFileNames();
if (fileNames.isEmpty()) {
System.out.println("No files found in the directory.");
} else {
System.out.println("Files in the directory:");
for (String fileName : fileNames) {
System.out.println(fileName);
}
}
} catch (IOException e) {
System.err.println("Error: An error occurred while accessing the directory - " + e.getMessage());
}
}
private List<String> receiveFileNames() {
List<String> fileNames = new ArrayList<>();
try {
Files.list(Paths.get(FEserver.FILES_PATH))
.forEach(path -> {
try {
fileNames.add(path.getFileName().toString());
} catch (Exception e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
// Send the list of file names to the server
fileNames.forEach(fileName -> {
try {
out.writeUTF(fileName);
} catch (IOException e) {
e.printStackTrace();
}
});
return fileNames;
}
}