-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiClientTest.java
More file actions
97 lines (82 loc) · 3.63 KB
/
MultiClientTest.java
File metadata and controls
97 lines (82 loc) · 3.63 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
import java.io.*;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MultiClientTest {
private static final String HOSTNAME = "localhost"; // 127.0.0.1
private static final int PORT = 12346;
private static final int NUM_CLIENTS = 3;
private static final String FILE_TO_GET = "notes.txt";
public static void main(String[] args) {
System.out.println("Starting " + NUM_CLIENTS + " concurrent clients...");
ExecutorService executor = Executors.newFixedThreadPool(NUM_CLIENTS);
for (int i = 1; i <= NUM_CLIENTS; i++) {
final int clientId = i;
executor.submit(() -> {
try {
runClient(clientId);
} catch (Exception e) {
System.err.println("Client " + clientId + " error: " + e.getMessage());
}
});
}
executor.shutdown();
try {
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
System.out.println("Multi-client test finished.");
}
private static void runClient(int clientId) throws IOException {
System.out.println("Client " + clientId + ": Connecting...");
try (Socket socket = new Socket(HOSTNAME, PORT)) {
// FIX: Un seul stream de base, partagé entre BufferedReader et DataInputStream
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(dataIn));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 1. LIST
System.out.println("Client " + clientId + ": Sending LIST...");
out.println("LIST");
String line;
// FIX: Lire jusqu'à ligne vide (pas null)
while ((line = in.readLine()) != null && !line.isEmpty()) {
System.out.println("Client " + clientId + " [LIST]: " + line);
}
// 2. GET
System.out.println("Client " + clientId + ": Sending GET " + FILE_TO_GET + "...");
out.println("GET " + FILE_TO_GET);
receiveFile(FILE_TO_GET + "_client" + clientId, dataIn);
// 3. QUIT
System.out.println("Client " + clientId + ": Sending QUIT...");
out.println("QUIT");
}
System.out.println("Client " + clientId + ": Test completed successfully.");
}
private static void receiveFile(String fileName, DataInputStream dataIn) throws IOException {
long fileSize = dataIn.readLong();
if (fileSize == -1) {
System.err.println("File not found: " + fileName);
return;
}
File downloadsDir = new File("downloads");
if (!downloadsDir.exists()) downloadsDir.mkdirs();
File file = new File(downloadsDir, fileName);
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] buffer = new byte[4096];
long totalRead = 0;
int bytesRead;
while (totalRead < fileSize &&
(bytesRead = dataIn.read(buffer, 0,
(int) Math.min(buffer.length, fileSize - totalRead))) != -1) {
fos.write(buffer, 0, bytesRead);
totalRead += bytesRead;
}
System.out.println("Client downloaded: " + file.getPath());
}
}
}