Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedi6431 authored Jun 8, 2024
1 parent 7abbb7b commit 29b6387
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Local_Host_Executer.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added out/production/Local_Host_Executer/Client.class
Binary file not shown.
Binary file added out/production/Local_Host_Executer/Server.class
Binary file not shown.
55 changes: 55 additions & 0 deletions src/Client.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
54 changes: 54 additions & 0 deletions src/Server.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 29b6387

Please sign in to comment.