-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloClient.java
More file actions
49 lines (39 loc) · 1.67 KB
/
HelloClient.java
File metadata and controls
49 lines (39 loc) · 1.67 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
// HelloClient.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;
import java.util.Scanner;
public class HelloClient {
public static void main(String[] args) {
try {
// Get the registry
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
// Look up the remote object
HelloInterface stub = (HelloInterface) registry.lookup("HelloService");
// Send messages to server
Scanner scanner = new Scanner(System.in);
System.out.println("\n--- Send Messages to Server ---");
System.out.println("Type your messages (type 'quit' to stop):");
while (true) {
System.out.print("> ");
String message = scanner.nextLine();
if (message.equalsIgnoreCase("quit")) {
break;
}
// Send message to server
stub.sendMessage(message);
System.out.println("Message sent to server!");
}
// Retrieve all messages from server
System.out.println("\n--- All Messages on Server ---");
List<String> allMessages = stub.getAllMessages();
for (int i = 0; i < allMessages.size(); i++) {
System.out.println((i + 1) + ". " + allMessages.get(i));
}
scanner.close();
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}