-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPServer.java
More file actions
100 lines (90 loc) · 4.74 KB
/
TCPServer.java
File metadata and controls
100 lines (90 loc) · 4.74 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
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 TCPServer {
private static Map<String, String> aliases = new HashMap<>();
public static void main(String argv[]) throws Exception {
int port = Integer.parseInt(argv[0]);
ServerSocket welcomeSocket = new ServerSocket(port);
System.out.println("Server is running...");
while (true) {
Socket connectionSocket = welcomeSocket.accept();
new Thread(new ClientHandler(connectionSocket)).start();
}
}
static class ClientHandler implements Runnable {
private Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
System.out.println(socket.getRemoteSocketAddress() + ": Joined the server");
}
@Override
public void run() {
try {
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
DataInputStream inStream = new DataInputStream(socket.getInputStream());
String clientSentence;
String alias = null;
while (true) {
clientSentence = inFromClient.readLine();
if (clientSentence == null) {
System.out.println((alias != null ? alias : socket.getRemoteSocketAddress()) + ": Client disconnected");
break;
} else if (clientSentence.startsWith("/register ")) {
String requestedAlias = clientSentence.split(" ", 2)[1];
if (aliases.containsValue(requestedAlias)) {
outToClient.writeBytes("Error: Registration failed. Handle or alias already exists.\n");
} else {
alias = requestedAlias;
aliases.put(socket.getRemoteSocketAddress().toString(), alias);
outToClient.writeBytes("Welcome " + alias + "!\n");
System.out.println(alias + ": Client registered");
}
} else if (clientSentence.startsWith("/join")) {
System.out.println(socket.getRemoteSocketAddress() + ": Joined the server");
} else if (clientSentence.startsWith("/store ")) {
String[] parts = clientSentence.split(" ");
if (parts.length == 3) {
String filename = parts[1];
long size = Long.parseLong(parts[2]);
byte[] data = new byte[(int)size];
inStream.readFully(data);
Path filePath = Paths.get("ServerFiles", filename);
Files.createDirectories(filePath.getParent());
Files.write(filePath, data);
System.out.println((alias != null ? alias : socket.getRemoteSocketAddress()) + ": Uploaded " + filename);
outToClient.writeBytes("File stored successfully\n");
}
} else if (clientSentence.equals("/dir")) {
File dir = new File("ServerFiles");
File[] files = dir.listFiles();
if (files != null) {
//outToClient.writeBytes(files.length + "\n");
for (File file : files) {
outToClient.writeBytes(file.getName() + '\n');
}
} else {
outToClient.writeBytes("0\n");
}
} else if (clientSentence.startsWith("/get ")) {
String filename = clientSentence.split(" ", 2)[1];
try {
Path path = Paths.get("ServerFiles/" + filename);
byte[] data = Files.readAllBytes(path);
Files.write(Paths.get(filename), data);
System.out.println("File " + filename + " has been sent to the client.");
} catch (IOException e) {
System.out.println("Error: Failed to send file.");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}