Browse Source

Änderungen 05.12.2023 um 14:44

master
leonmcfly 11 months ago
parent
commit
5d740240de
1 changed files with 67 additions and 0 deletions
  1. 67
    0
      src/DistributedSorter/SorterServer.java

+ 67
- 0
src/DistributedSorter/SorterServer.java View File

@@ -0,0 +1,67 @@
package DistributedSorter;

import jdk.jshell.spi.ExecutionControl;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

public class SorterServer implements Runnable {

Socket client;
public SorterServer(Socket client) {
this.client = client;
}

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());
}
}

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 receiveLine(Socket socket) throws IOException {
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
return reader.readLine();
}


private String sort(String line){
char[] chars = line.toCharArray();
Arrays.sort(chars);
return new String(chars);
}

@Override
public void run() {
try{
String line = receiveLine(client);
String answer = sort(line);
sendLine(client, answer);
System.out.println(line + "-->"+ answer);
} catch (IOException e) {
System.err.println("Fehler");
}
finally{
try{client.close();} catch(Exception e)
{}
}
}
}

Loading…
Cancel
Save