-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPClient.java
More file actions
174 lines (161 loc) · 8.1 KB
/
TCPClient.java
File metadata and controls
174 lines (161 loc) · 8.1 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
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
class TCPClient {
public static void main(String argv[]) throws Exception {
boolean isConnected = false;
String serverIp = null;
int port = 0;
Socket clientSocket = null;
DataOutputStream outToServer = null;
String alias = null;
Scanner scanner = new Scanner(System.in);
System.out.println("Client is running...");
while (true) {
String sentence = scanner.nextLine();
if (sentence.trim().isEmpty()) {
continue;
}
if (clientSocket == null && !sentence.startsWith("/join")) {
System.out.println("Error: Command failed. Please connect to the server first.");
continue;
}
if (sentence.startsWith("/join")) {
String[] parts = sentence.split(" ");
if (parts.length != 3) {
System.out.println("Error: Command parameters do not match or is not allowed.");
continue;
}
if (isConnected) {
System.out.println("Error: You are already connected to the server.");
continue;
}
serverIp = parts[1];
port = Integer.parseInt(parts[2]);
try {
clientSocket = new Socket(serverIp, port);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
new Thread(new ServerHandler(clientSocket)).start();
isConnected = true; // Set isConnected to true after successfully connecting to the server
System.out.println("Connection to the File Exchange Server is successful!");
continue;
} catch (IOException e) {
System.out.println("Error: Connection to the Server has failed! Please check IP Address and Port Number.");
}
}
if (sentence.equals("/?") || sentence.equals("/leave") || sentence.startsWith("/register ") || sentence.startsWith("/store ") || sentence.equals("/dir") || sentence.startsWith("/get ")) {
if (sentence.equals("/?")) {
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("/? - Request command help to output all Input Syntax commands for references");
} else if (sentence.startsWith("/register ")) {
if (clientSocket == null) {
System.out.println("Error: Registration failed. Handle or alias already exists.");
continue;
}
alias = sentence.split(" ", 2)[1];
outToServer.writeBytes(sentence + '\n');
} else if (sentence.startsWith("/store ")) {
if (clientSocket == null) {
System.out.println("Error: Please connect to the server first.");
continue;
}
if (alias == null) {
System.out.println("Error: Please register first.");
continue;
}
String filename = sentence.split(" ", 2)[1];
try {
Path path = Paths.get(filename);
byte[] data = Files.readAllBytes(path);
outToServer.writeBytes(sentence + ' ' + data.length + '\n');
outToServer.write(data, 0, data.length);
System.out.println("File " + filename + " has been sent to the server.");
} catch (IOException e) {
System.out.println("Error: File not found.");
}
} else if (sentence.equals("/dir")) {
if (clientSocket == null) {
System.out.println("Error: Please connect to the server first.");
continue;
}
if (alias == null) {
System.out.println("Error: Please register first.");
continue;
}
System.out.println("Server Directory");
outToServer.writeBytes(sentence + '\n');
} else if (sentence.startsWith("/get ")) {
if (clientSocket == null) {
System.out.println("Error: Please connect to the server first.");
continue;
}
if (alias == null) {
System.out.println("Error: Please register first.");
continue;
}
String filename = sentence.split(" ", 2)[1];
outToServer.writeBytes(sentence + '\n');
try {
Path path = Paths.get("ServerFiles/" + filename);
byte[] data = Files.readAllBytes(path);
Files.write(Paths.get(filename), data);
System.out.println("File received from Server: " + filename);
} catch (IOException e) {
System.out.println("Error: File not found in the server.");
}
} else if (sentence.equals("/leave")) {
if (clientSocket == null) {
System.out.println("Error: Disconnection failed. Please connect to the server first.");
continue;
}
clientSocket.close();
System.out.println("Connection closed. Thank you!");
break;
}
} else {
System.out.println("Error: Command not found.");
}
}
}
static class ServerHandler implements Runnable {
private Socket socket;
public ServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
DataInputStream inStream = new DataInputStream(socket.getInputStream());
while (true) {
String serverSentence = inFromServer.readLine();
if (serverSentence == null) {
System.out.println("Server has disconnected.");
socket.close();
break;
} else if (serverSentence.startsWith("/dir ")) {
int count = inStream.readInt();
System.out.println("Directory listing:");
for (int i = 0; i < count; i++) {
System.out.println(inFromServer.readLine());
}
} else if (serverSentence.startsWith("Stored file ")) {
System.out.println(serverSentence);
} else {
System.out.println(serverSentence);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}