123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Scanner;
-
- public class DistributedSorterClient {
- public static void main(String[] args) {
- new DistributedSorterClient().run();
- }
-
- private void run() {
- Scanner scanner = new Scanner(System.in);
- while (true) {
- System.out.print("Enter a string to sort (or press Enter to quit): ");
- String line = scanner.nextLine();
- if (line.isEmpty()) break;
-
- // Senden und Empfangen der sortierten Zeile
- String sortedLine = sort(line);
- if (sortedLine != null) {
- System.out.println("Sorted: " + sortedLine);
- } else {
- System.out.println("Error communicating with server.");
- }
- }
- scanner.close();
- }
-
- private String sort(String line) {
- String response = "";
- try (Socket socket = new Socket("localhost", 12345);
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
-
- // Sende die Eingabezeile an den Server
- out.println(line);
-
- // Warte auf die Antwort des Servers
- response = in.readLine();
-
- } catch (Exception e) {
- System.err.println("Error communicating with server: " + e.getMessage());
- return null;
- }
- return response;
- }
- }
|