Änderungen vom 05.12.2023 um 17:00

This commit is contained in:
Leon Schubert 2023-12-05 17:00:23 +01:00
parent f6668dfce7
commit c117379558
4 changed files with 90 additions and 13 deletions

1
.idea/misc.xml generated
View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -1,13 +1,29 @@
package Prog3.src.DistributedSorter;
import java.io.*;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;
public class SorterClient {
public class SorterClient implements Runnable{
public static void main(String[] args) {
SorterClient client = new SorterClient();
client.run();
}
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();
}
public void run(){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
@ -20,7 +36,13 @@ public class SorterClient {
scanner.close();
}
private String sort(String line){
private String sort(String line) {
try (Socket socket = new Socket("localhost", 12345)) {
sendLine(socket, line);
receiveLine(socket);
} catch (Exception e) {
return "Error connecting to the server.";
}
}
}

View File

@ -0,0 +1,66 @@
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);
} catch (IOException e) {
System.err.println("Fehler");
}
finally{
try{client.close();} catch(Exception e)
{}
}
}
}