Java执行ipconfig,Client-Server

时间:2014-04-27 13:27:56

标签: java shell

我创建一个在客户端中键入ifconfig(Linux)或ipconfig(Windows)的程序,然后它发送到服务器并执行shell。

它执行正常,在服务器中我可以正确看到ipconfig,但我不知道如何将服务器的结果返回给客户端。

客户端:

public class ShellClient {  
public static void main(String[] args) throws IOException {
        String comand;
        Socket server =null;
        server = new Socket("localhost", 1234);
        while(true){
        System.out.println("Insert ipconfig(windows)/ ifconfig(Linux): ");
        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);

        comando = keyboard.next();

        try{
             System.out.println("Conecting...");
             DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
             DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));

                out.writeUTF(comand);
                out.flush();
                String result = in.readUTF();
                System.out.println(result);


                if (comand == "0"){server.close();
                System.out.println("Finish. Thank You!");
                System.exit(0);}

        }catch(IOException ioe){
            System.err.println(ioe);
        }
      } 
    }
  }

服务器端:

public class ShellServer {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = null;
        Socket client = null;
        String result;
        String comand ;
        String s = null;
        socket = new ServerSocket(1234);  

        System.out.println("The server keep working...");
        client = socket.accept();
        System.out.println("The client is connected");
        DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));

            comand = in.readUTF();
            result = Shell.CheckCommand(comand);
            //out.writeUTF(result);
            //out.flush();

         // Ejcutamos el comando
            Process p = Runtime.getRuntime().exec(result);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                            p.getInputStream()));

            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
        }

            if(comand == "0"){client.close();
                         socket.close();}
            }
        }

先谢谢你们!

1 个答案:

答案 0 :(得分:1)

服务器

当您调用进程并从输入流中读取时,将您收到的内容发送到流出。现在客户端根本没有收到任何东西,因此无法显示任何内容。我会使用BufferedReader(InputStreamReader)从客户端读取。

PrintWriter out = new PrintWriter(server.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));

out.println(comand);

String result;
while ((result = in.readLine()) != null )
{
    System.out.println(result);
}

<强>客户端: 只需从服务器流接收它发送的内容,例如:

PrintWriter out = new PrintWriter(server.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out.println(comand);

String result;
while ((result = in.readLine()) != null )
{
    System.out.println(result);
}