1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package Praktikum4;
-
-
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.HashMap;
- import java.util.Map;
-
- public class SorterServer implements Runnable {
- public static void main(String[] args) {
- final int port = 12345;
- try (ServerSocket socket = new ServerSocket(port)) {
- while (true) {
- Socket client = socket.accept();
- Thread thread = new Thread(new SorterServer(client));
- thread.start();
- }
- } catch (Exception e) {
- System.err.println("Error: " + e.getMessage());
- }
- }
-
- private Socket socket;
-
- private SorterServer(Socket socket) {
- this.socket = socket;
- }
-
- @Override
- public void run() {
- try {
- String msg = receiveLine(this.socket);
- String ausgabe=sortAndCount(msg);
- sendLine(socket,ausgabe );
- }
- catch (Exception e) {};
- }
-
-
- public static String receiveLine(Socket socket) throws IOException {
- InputStream in = socket.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- return reader.readLine();
- }
-
- public static void sendLine(Socket socket, String line) throws IOException {
- OutputStream out = socket.getOutputStream();
- PrintWriter writer = new PrintWriter(out);
- writer.println(line);
- writer.flush();
- }
-
- public static String sortAndCount(String input) {
-
- Map<Character, Integer> charCountMap = new HashMap<>();
-
-
- for (char c : input.toCharArray()) {
- charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
- }
-
-
- StringBuilder sortedString = new StringBuilder();
-
-
- for (char c = 'a'; c <= 'z'; c++) {
-
- if (charCountMap.containsKey(c)) {
- int count = charCountMap.get(c);
- for (int i = 0; i < count; i++) {
- sortedString.append(c);
- }
- }
- }
-
- // Rückgabe der sortierten Zeichenkette
- return sortedString.toString();
- }
- }
|