<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | <project version="4"> | ||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="corretto-17" project-jdk-type="JavaSDK"> | <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" /> | <output url="file://$PROJECT_DIR$/out" /> |
<?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> |
package Prog3.src.DistributedSorter; | package Prog3.src.DistributedSorter; | ||||
import java.io.*; | |||||
import java.net.Socket; | |||||
import java.util.Arrays; | |||||
import java.util.Scanner; | import java.util.Scanner; | ||||
public class SorterClient { | |||||
public class SorterClient implements Runnable{ | |||||
public static void main(String[] args) { | public static void main(String[] args) { | ||||
SorterClient client = new SorterClient(); | SorterClient client = new SorterClient(); | ||||
client.run(); | 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(){ | public void run(){ | ||||
Scanner scanner = new Scanner(System.in); | Scanner scanner = new Scanner(System.in); | ||||
while (scanner.hasNextLine()) { | while (scanner.hasNextLine()) { | ||||
scanner.close(); | 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."; | |||||
} | |||||
} | } | ||||
} | } |
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) | |||||
{} | |||||
} | |||||
} | |||||
} |