-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
37 lines (32 loc) · 1.52 KB
/
Main.java
File metadata and controls
37 lines (32 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
class Main {
// Command that we want to execute in terminal
private final static String command = "bash GetTemperature.sh";
public static void main(String[] args) throws IOException {
// Creating server socket
ServerSocket serverSocket = new ServerSocket(7778);
// Infinite loop
while (true) {
try {
// Accepting connection
Socket socket = serverSocket.accept();
String linuxCommandResult = "";
// Executing command
Process p = Runtime.getRuntime().exec(command);
// Getting command output and sending it to client
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((linuxCommandResult = stdInput.readLine()) != null) {
OutputStream outputStream = socket.getOutputStream();
// Create an object output stream from the output stream so we can send an object through it
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
// Convert to double because we should get string like "40.0"
objectOutputStream.writeObject(Double.valueOf(linuxCommandResult));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}