|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
{} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |