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 24, 2024
1 parent cfd7926 commit f2324e0
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//This software is protected by Fedi6431© copyrights
import java.net.*;
import java.io.*;

public class Server {
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream input = null;

public Server(int port) {
try {
// create a server socket
server = new ServerSocket(port);
System.out.println("Made by Fedi6431");
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()));


// string to read input
String line = "";

line = input.readUTF();
boolean linux = Boolean.parseBoolean(line);

if (linux == true) {
while (!line.equalsIgnoreCase("exit")) {
try {
// read the message sent by the client via socket
line = input.readUTF();
System.out.println(line);

// try to execute the command on terminal using Runtime.getRuntime
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());
}
}
} else if (linux == false) {
while (!line.equalsIgnoreCase("exit")) {
try {
// read the message sent by the client via socket
line = input.readUTF();
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});
} catch (IOException IOe) {
System.out.println("Oops.. Something went wrong.");
System.out.println("IO exception: " + IOe.getMessage());
System.out.println("Exception cause" + IOe.getCause());
}
}
}


// close the connection
System.out.println("Made by Fedi6431");
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 f2324e0

Please sign in to comment.