1

我有 2 个应用程序 - 一个服务器和一个客户端。基本上我想要做的是从我的客户端应用程序远程控制服务器机器上的 cmd.exe - 只需将客户端输入传送到 cmd.exe 并将 cmd.exe 的输出传送到客户端应用程序。

一开始我收到:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

所以一切似乎都很好,但我无法从客户端应用程序向 cmd.exe 发送命令。服务器应用程序收到 cmd,例如“帮助”,但命令没有到达 cmd.exe,我不知道为什么。我希望有人能够帮助我解决这个问题。

服务器代码

package net.api.windows.command.shell;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.ProcessBuilder.Redirect;
import java.net.ServerSocket;
import java.net.Socket;

public class CommandShellServer implements Runnable {

// - Init - //
private Socket socket;
private ServerSocket serverSocket;
private InputStream IN;
private OutputStream OUT;
private ObjectInputStream ObjIN;
private ObjectOutputStream ObjOUT;

// ___________________________________ //

public static void main(String[] args) {
    new CommandShellServer();
}

public CommandShellServer() {
    Thread thread = new Thread(this);
    thread.start();
}

@Override
public void run() {
    try {
        System.out.println("Starting server..");
        serverSocket = new ServerSocket(65000);
        System.out.println("Server started!");
        socket = serverSocket.accept();
        System.out.println("\n" + "Client connected!");

        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe").redirectInput(Redirect.PIPE)
                .redirectOutput(Redirect.PIPE).redirectError(Redirect.PIPE);
        Process process = processBuilder.start();
        final BufferedReader ProcessIN = new BufferedReader(new InputStreamReader(process.getInputStream()));
        final BufferedReader ProcessERROR = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        final BufferedWriter ProcessOUT = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

        // Socket OutputStream
        Thread P_OUT = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while(true) {
                        String x = "";
                        if((x = ProcessIN.readLine()) != null) {
                            System.out.println("Process says: " + x);
                            OUT = socket.getOutputStream();
                            ObjOUT = new ObjectOutputStream(OUT);

                            ObjOUT.writeObject(x);
                            ObjOUT.flush();
                        }
                    }
                } catch(IOException e) {
                    System.out.println(e.getMessage().toString());
                }
            }
        });
        P_OUT.start();

        // Socket Error OutputStream
        Thread P_ERROR = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while(true) {
                        String x = "";
                        if((x = ProcessERROR.readLine()) != null) {
                            OUT = socket.getOutputStream();
                            ObjOUT = new ObjectOutputStream(OUT);

                            ObjOUT.writeObject(x);
                            ObjOUT.flush();
                        }
                    }
                } catch(IOException e) {
                    System.out.println(e.getMessage().toString());
                }
            }
        });
        P_ERROR.start();

        // Socket InputStream
        Thread P_IN = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while(true) {
                        IN = socket.getInputStream();
                        ObjIN = new ObjectInputStream(IN);

                        String x = "";
                        if((x = ObjIN.readObject().toString()) != null) {
                            System.out.println("Client says: " + x);
                            ProcessOUT.write(x);
                            ProcessOUT.flush();
                        }
                    }
                } catch(IOException e1) {
                    System.out.println(e1.getMessage().toString());
                } catch(ClassNotFoundException e2) {
                    System.out.println(e2.getMessage().toString());
                }
            }
        });
        P_IN.start();

    } catch(IOException e1) {
        System.out.println(e1.getMessage().toString());
    }
}
}

客户代码

package net.api.windows.command.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class CommandShellClient {

// - Init - //
private static Socket socket;
private static InputStream IN;
private static OutputStream OUT;
private static ObjectInputStream ObjIN;
private static ObjectOutputStream ObjOUT;

// ___________________________________ //

public static void main(String[] args) {
    try {
        System.out.println("Connceting to server..");
        SocketAddress sockAddr = new InetSocketAddress("192.168.1.21", 65000);
        socket = new Socket();
        socket.connect(sockAddr, 5000);

        if(socket.isConnected()) {
            System.out.println("Connected!" + "\n");

            // Socket InputStream
            Thread Thread_IN = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        while(true) {
                            IN = socket.getInputStream();
                            ObjIN = new ObjectInputStream(IN);

                            String x = "";
                            if((x = ObjIN.readObject().toString()) != null) {
                                System.out.println("Process says: " + x);
                            }
                        }
                    } catch(IOException e1) {
                        System.out.println(e1.getMessage().toString());
                    } catch(ClassNotFoundException e2) {
                        System.out.println(e2.getMessage().toString());
                    }
                }
            });
            Thread_IN.start();

            // Socket OutputStream
            Thread Thread_OUT = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        BufferedReader SYSTEM_IN = new BufferedReader(new InputStreamReader(System.in));

                        while(true) {
                            String x = "";
                            if((x = SYSTEM_IN.readLine()) != null) {
                                OUT = socket.getOutputStream();
                                ObjOUT = new ObjectOutputStream(OUT);

                                ObjOUT.writeObject(x);
                                ObjOUT.flush();
                            }
                        }
                    } catch(IOException e1) {
                        System.out.println(e1.getMessage().toString());
                    }
                }
            });
            Thread_OUT.start();
        }
    } catch(IOException e1) {
        System.out.println(e1.getMessage().toString());
    }
}
}

解决方案

在服务器代码中,在最后一个线程 (P_IN) 中,您必须键入:

ProcessOUT.write(x);
ProcessOUT.newLine();
ProcessOUT.flush();

..代替:

ProcessOUT.write(x);
ProcessOUT.flush();

ProcessOUT.newLine() 很重要

4

0 回答 0