我编写了一个Java TCP
服务器程序,用于从客户端关闭我的PC。它正在成功接收来自客户端的消息,但它没有关闭PC
有人请帮我解决这个问题
这是我的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSideSocket {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(4020); //Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4020");
}
System.out.println("Server started. Listening to the port 4020");
while (true) {
try {
clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();
if(message=="shutdown"){
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t ");
System.exit(0);
}
else if(message=="restart"){
Runtime runtime1 = Runtime.getRuntime();
Process proc2 = runtime1.exec("shutdown -r -t ");
System.exit(0);
}
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
答案 0 :(得分:3)
关闭的参数-t需要一个数字(等待直到关闭/重启的秒数)
答案 1 :(得分:3)
尝试sudo poweroff
并阅读流程中的输出/错误,以便了解错误。
此外,您需要使用equals
而不是==
来比较字符串。
顺便说一句如果您想要做的就是远程关机,您可以像这样使用SSH
ssh host-to-shitdown sudo poweroff
这更安全,您不需要编写任何Java代码。
答案 2 :(得分:1)
编辑代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSideSocket {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(4020); //Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4020");
}
System.out.println("Server started. Listening to the port 4020");
while (true) {
try {
clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();
if(message.equals("shutdown")){
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 60");
System.exit(0);
}
else if(message.equals("restart")){
Runtime runtime1 = Runtime.getRuntime();
Process proc2 = runtime1.exec("shutdown -r -t 60");
System.exit(0);
}
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
在上面的代码中,我更改了要执行的命令,您需要在此处指定关闭时间,同时还要比较您需要遵循的字符串,如上所述。