Skip to content

Commit

Permalink
Update Server.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedi6431 authored Jul 7, 2024
1 parent a9da4d5 commit 44ba177
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,51 +40,50 @@ 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
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});
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();
input.close();
}
// 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()}");
}
}
}

0 comments on commit 44ba177

Please sign in to comment.