From 44ba177c0bf3295b83e878eb6049068eca6545f6 Mon Sep 17 00:00:00 2001 From: Fedi6431 <102946457+Fedi6431@users.noreply.github.com> Date: Sun, 7 Jul 2024 14:44:19 +0200 Subject: [PATCH] Update Server.java --- src/Server.java | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Server.java b/src/Server.java index 5d298a9..b2a1d3f 100644 --- a/src/Server.java +++ b/src/Server.java @@ -7,33 +7,29 @@ public class Server { public static void Initialize(String address, int port) { try { // create a server socket - InetAddress localIpAddress = InetAddress.getLocalHost(); System.out.println("Made by Fedi6431"); + ServerSocket server = new ServerSocket(); System.out.println("Server started"); - server.bind(new InetSocketAddress(localIpAddress, 65000)); - System.out.println("Running on port " + port + ", address: " + localIpAddress); + server.bind(new InetSocketAddress(localIpAddress, 65000)); + System.out.println(STR."Running on port \{port}, address: \{localIpAddress}"); // establish a connection with the client Socket socket = server.accept(); System.out.println("Client connected"); - // get input from the terminal DataInputStream input = new DataInputStream( new BufferedInputStream(socket.getInputStream())); - // string to read input String line = ""; - line = input.readUTF(); - boolean linux = Boolean.parseBoolean(line); - - if (linux == true) { + String os = System.getProperty("os.name"); + if (os.equals("windows")) { while (!line.equalsIgnoreCase("exit")) { try { // read the message sent by the client via socket @@ -44,11 +40,11 @@ public static void Initialize(String address, int port) { Process linux_process = Runtime.getRuntime().exec(new String[]{line}); } catch (IOException IOe) { System.out.println("Oops.. Something went wrong."); - System.out.println("IO exception: " + IOe.getMessage()); - System.out.println("Exception cause" + IOe.getCause()); + System.out.println(STR."IO exception: \{IOe.getMessage()}"); + System.out.println(STR."Exception cause\{IOe.getCause()}"); } } - } else if (linux == false) { + } else { while (!line.equalsIgnoreCase("exit")) { try { // read the message sent by the client via socket @@ -56,16 +52,15 @@ public static void Initialize(String address, int port) { System.out.println(line); // try to execute the command on cmd using Runtime.getRuntime - Process windows_process = Runtime.getRuntime().exec(new String[]{"cmd", "/c " + line}); + Process windows_process = Runtime.getRuntime().exec(new String[]{"cmd", STR."/c \{line}"}); } catch (IOException IOe) { System.out.println("Oops.. Something went wrong."); - System.out.println("IO exception: " + IOe.getMessage()); - System.out.println("Exception cause" + IOe.getCause()); + System.out.println(STR."IO exception: \{IOe.getMessage()}"); + System.out.println(STR."Exception cause\{IOe.getCause()}"); } } } - // close the connection System.out.println("Made by Fedi6431"); socket.close(); @@ -73,22 +68,22 @@ public static void Initialize(String address, int port) { } // handle any errors catch (IOException IOe) { - System.out.println("IO exception: " + IOe.getMessage()); - System.out.println("Exception cause" + IOe.getCause()); + System.out.println(STR."IO exception: \{IOe.getMessage()}"); + System.out.println(STR."Exception cause\{IOe.getCause()}"); } catch (RuntimeException RTe) { - System.out.println("Runtime exception: " + RTe.getMessage()); - System.out.println("Exception cause" + RTe.getCause()); + System.out.println(STR."Runtime exception: \{RTe.getMessage()}"); + System.out.println(STR."Exception cause\{RTe.getCause()}"); } } - public static void main(String args[]) { + public static void main(String[] args) { try { InetAddress localIpAddress = InetAddress.getLocalHost(); String address = localIpAddress.getHostAddress(); Initialize(address,65000); } catch (UnknownHostException e) { - System.out.println("Error getting local host address: " + e.getMessage()); + System.out.println(STR."Error getting local host address: \{e.getMessage()}"); } } }