diff --git a/Local_Host_Executer.iml b/Local_Host_Executer.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/Local_Host_Executer.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Local_Host_Executer/Client.class b/out/production/Local_Host_Executer/Client.class new file mode 100644 index 0000000..9c7457d Binary files /dev/null and b/out/production/Local_Host_Executer/Client.class differ diff --git a/out/production/Local_Host_Executer/Server.class b/out/production/Local_Host_Executer/Server.class new file mode 100644 index 0000000..852c292 Binary files /dev/null and b/out/production/Local_Host_Executer/Server.class differ diff --git a/src/Client.java b/src/Client.java new file mode 100644 index 0000000..0bdf706 --- /dev/null +++ b/src/Client.java @@ -0,0 +1,55 @@ +import java.io.*; +import java.net.*; + +public class Client { + private Socket socket = null; + private DataInputStream input = null; + private DataOutputStream output = null; + + public Client(String address, int port) { + try { + // establish a connection with the server + System.out.println("Client started"); + socket = new Socket(address, port); + System.out.println("Client connected"); + + // get input from the terminal + input = new DataInputStream(System.in); + // send output to the server socket + output = new DataOutputStream(socket.getOutputStream()); + + // string to read input + String line = ""; + + // IP of the server and the client + String serverIP = socket.getLocalAddress().getHostAddress(); + String clientIP = Inet4Address.getLocalHost().getHostAddress(); + + // loop until "exit" is written + while (!line.equalsIgnoreCase("exit")) { + // get user input + System.out.print(clientIP + " to " + serverIP + "$-"); + line = input.readLine(); + // send the received input to the server + output.writeUTF(line); + } + // close the connection + input.close(); + output.close(); + socket.close(); + } + // handle any errors + catch (IOException IOe) { + System.out.println("IO exception: " + IOe.getMessage()); + System.out.println("Exception cause" + IOe.getCause()); + } + catch (RuntimeException RTe) { + System.out.println("Runtime exception: " + RTe.getMessage()); + System.out.println("Exception cause" + RTe.getCause()); + } + } + + public static void main(String args[]) { + Client client = new Client("127.0.0.1", 5000); + } +} diff --git a/src/Server.java b/src/Server.java new file mode 100644 index 0000000..2b5e098 --- /dev/null +++ b/src/Server.java @@ -0,0 +1,54 @@ +import java.net.*; +import java.io.*; + +public class Server { + private Socket socket = null; + private ServerSocket server = null; + private DataInputStream input = null; + private DataOutputStream output = null; + + public Server(int port) { + try { + // create a server socket + server = new ServerSocket(port); + System.out.println("Server started"); + // establish a connection with the client + socket = server.accept(); + System.out.println("Client connected"); + + // get input from the terminal + input = new DataInputStream( + new BufferedInputStream(socket.getInputStream())); + // send output to the client + output = new DataOutputStream( + new BufferedOutputStream(socket.getOutputStream())); + + // string to read input + String line = ""; + while (!line.equalsIgnoreCase("exit")) { + // read the message sent by the client via socket + line = input.readUTF(); + System.out.println(line); + String string_cod = line; + // try to execute the command on cmd using Runtime.getRuntime + Process cmd_process = Runtime.getRuntime().exec(new String[]{"cmd", "/c " + string_cod}); + } + // close the connection + socket.close(); + input.close(); + } + // handle any errors + catch (IOException IOe) { + System.out.println("IO exception: " + IOe.getMessage()); + System.out.println("Exception cause" + IOe.getCause()); + } + catch (RuntimeException RTe) { + System.out.println("Runtime exception: " + RTe.getMessage()); + System.out.println("Exception cause" + RTe.getCause()); + } + } + + public static void main(String args[]) { + Server server = new Server(5000); + } +}