Repository zur Vorlesung Prog3A
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DistributedSorterClient.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.PrintWriter;
  4. import java.net.Socket;
  5. import java.util.Scanner;
  6. public class DistributedSorterClient {
  7. public static void main(String[] args) {
  8. new DistributedSorterClient().run();
  9. }
  10. private void run() {
  11. Scanner scanner = new Scanner(System.in);
  12. while (true) {
  13. System.out.print("Enter a string to sort (or press Enter to quit): ");
  14. String line = scanner.nextLine();
  15. if (line.isEmpty()) break;
  16. // Senden und Empfangen der sortierten Zeile
  17. String sortedLine = sort(line);
  18. if (sortedLine != null) {
  19. System.out.println("Sorted: " + sortedLine);
  20. } else {
  21. System.out.println("Error communicating with server.");
  22. }
  23. }
  24. scanner.close();
  25. }
  26. private String sort(String line) {
  27. String response = "";
  28. try (Socket socket = new Socket("localhost", 12345);
  29. PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
  30. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
  31. // Sende die Eingabezeile an den Server
  32. out.println(line);
  33. // Warte auf die Antwort des Servers
  34. response = in.readLine();
  35. } catch (Exception e) {
  36. System.err.println("Error communicating with server: " + e.getMessage());
  37. return null;
  38. }
  39. return response;
  40. }
  41. }