简体   繁体   English

rmi java程序未预期输出

[英]rmi java program not expected output

The rmi program when run asks a command. rmi程序在运行时会询问命令。

If I gave ipconfig it works,but does not works for ping www.google.com and powercfg/batteryreport etc. I think it does not works if their is a space or slash between the input command. 如果我提供了ipconfig,它可以工作,但不能ping www.google.com和powercfg / batteryreport等。如果输入命令之间有空格或斜杠,我认为它不起作用。

Any help would be appreciated 任何帮助,将不胜感激

si.java si.java

 import java.rmi.*;
 import java.net.*;
 import java.io.*; 

 public interface si extends Remote{
      public String calc(String com) throws RemoteException,IOException;
 }

imp.java imp.java

  import java.rmi.*;
  import java.rmi.server.*;
  import java.util.*;
  import java.net.*;
  import java.io.*;

  public class imp extends UnicastRemoteObject implements si{

  imp() throws RemoteException,IOException {
      super();
  }

  public String calc(String com) throws IOException{
      String inputLine;
      Runtime r=Runtime.getRuntime();
      Process p=r.exec(com);
      BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));

      while((inputLine = in.readLine()) != null) {
          System.out.println(inputLine);
          //pingResult += inputLine;
      }

      return inputLine;
   }

}

server.java 服务器.java

  import java.rmi.*;
  import java.rmi.registry.*;

  public class server{
       public static void main(String args[]){
           try{
               si stub=new imp();
               Naming.rebind("rmi://localhost:5000/example",stub);
           } catch(Exception e) {
           }
       }
 }

client.java 客户端程序

import java.rmi.*;
import java.util.*;
public class client{
    public static void main(String args[]){
       try{
             si a=(si)Naming.lookup("rmi://localhost:5000/example");
             Scanner s=new Scanner(System.in);
             System.out.println("enter command");
             String com=s.next();
             System.out.println(a.calc(com));
             } catch(Exception e){
             }
      }
}

Scanner by default parses whitespace-delimited tokens, so you are getting and sending to the server only the first part of the input line. Scanner默认情况下解析空格分隔的标记,因此您仅获取输入行的第一部分并将其发送到服务器。 The server can only execute what you send it. 服务器只能执行您发送的内容。

Use Scanner.nextLine() . 使用Scanner.nextLine() Or if you only want lines, you don't need Scanner ; 或者,如果您只需要线条,则不需要Scanner use BufferedReader and readLine() . 使用BufferedReaderreadLine()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM