java - 使用java rmi协议(protocol)将文本文件内容解析到客户端窗口?

标签 java rmi

我是 RMI 技术的新手,在用户连接到 RMI 服务器后,正在努力将文件内容解析到客户端命令窗口。

问题是我可以读取该文件,但它只读取第一行,而不读取文件内容的其余部分。有人可以查看我的代码并看看哪里出错了。

界面

/**
 * The purpose of this interface is to declare a set of remote methods 
 * and each remote method must declare RemoteException in its throws clause 
 * Here we are simple making this available to remote accesseser 
 * 
 * A remote interface defines a remote service.
 * The interface java.rmi.remote extend not interface or methods,
 * it is a marker interface which distinguishes remote interfaces from non-remote interfaces.*/

 import java.rmi.Remote;
 import java.rmi.RemoteException;

 public interface RemoteInterface extends Remote {

    /**
     * method returns a String message to its caller
     * @param str :value of String*/
     public String sayHello(String str) throws RemoteException;
     public String displayQuestions() throws RemoteException;
 }

远程对象

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.RemoteException;

 //remote object
 public class HelloImpl implements RemoteInterface {

@Override
public String sayHello(String str) throws RemoteException {
    return "Hello: " + str;
}

@Override
public String displayQuestions() throws RemoteException {

    try {
        BufferedReader br = new BufferedReader(new FileReader("questions.txt"));
        while(true){
            String line = br.readLine();
            if(line == null) break;
        return line;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
    }
}

RMI 服务器

 /**
 * The purpose of this class is to implement the server. 
 * This class will have a main() method that:-
 * 1- creates an instance(object of remote object) of the remote object implementation,
 * 2- then exports the remote object
 * 3- then register the remote object with a Java RMI registry
 * */

    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;

//RMI Server
public class HelloServer {

public HelloServer() {
}

public static void main(String[] args) {
    try {

        //Create an instance of the remote object
        //here remoteObj is an instance of remote object 'HelloImpl'
        HelloImpl remoteObj = new HelloImpl();

        //To Export the remote object, we will use 'UnicastRe...exportObject(remoteObj, TCPPortNo)' method
        //When you export a remote object, you make that object available to accept incoming calls from clients
        //If you pass a zero to the method, the default TCP port number 1099 is used.
        //Note that the exportObject() method will return a stub, which is a term used to describe a proxy class
        //The stub class is a key to make remote object available for remote invocation
        RemoteInterface stub = (RemoteInterface) UnicastRemoteObject
                .exportObject(remoteObj, 0);
        // Bind the remote object's stub in the registry
        // create a registry instance, this will get me the handle to the registry
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind("nameOfRObj", stub); // here we bind an instance of the object in the registry 
        System.out.println("Hello Server is ready to listen...");

    } catch (Exception e) {
        System.err.println("Server exception thrown" + e.toString());
        e.printStackTrace();
    }
}

  }

RMI 客户端

 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
 import java.util.ArrayList;
 import java.util.List;


 public class HelloClient {

public HelloClient(){}

public static void main(String[] args){

    //String mood = inProfit() ? "happy" : "sad";
    //if args length is lessthan 1, then assign the value localHost to field hostName
    //otherwise, assign args[0] thats passed to hostName
    String hostName = (args.length < 1) ? "localHost" : args[0];
    try {
        //Locate a host from the registry mechanism.
        Registry registry = LocateRegistry.getRegistry(hostName);
        //look up the remote object by its name
        RemoteInterface stub = (RemoteInterface) registry.lookup("nameOfRObj"); 



        String name = stub.sayHello("Betty");
        System.out.println("Got info from server: " + " " + name);

        List<String> list =  new ArrayList<String>();
        list.add(stub.displayQuestions());

        for(String line : list)
        System.out.println("Content" + line);

    } catch (Exception e) {
        System.out.println("Client exception thrown: " + e.toString());
        e.printStackTrace();
    }
}

}

文本文件内容

Questions database:
Q1: (A + B)*(A+B)
1. A*A + B*B
2. A*A +A*B + B*B
3. A*A +2*A*B + B*B  
Q2: (A + B)*(A - B)
1. A*A + 2*B*B
2. A*A - B*B 
3. A*A -2*A*B + B*B
Q3: sin(x)*sin(x) + cos(x)*cos(x)
1. 1 
2. 2
3. 3

当前结果

D:\rmi>java HelloClient
Got info from server:  Hello: Betty
ContentQuestions database:

预期结果

客户端窗口上一次应出现一个问题,当他/她回答第一个问题时,会出现第二个问题,然后出现第三个问题,以及回答正确问题的最终分数。

例如在客户端窗口:
请选择正确答案
Q1:布拉拉拉?
1:A
2:b
3:c
输入:

请选择正确答案
Q2:布拉拉拉?
1:A
2:b
3:c
输入:b
....

最终得分 2 可能 3 Q
再见!

最佳答案

你的 while 循环中有一个 return - 确保只有一次迭代会以这种方式处理...... 试试这个:

String content = "";
while(true){
   String line = br.readLine();
    if(line == null) break;
    content += line;
}
return content;

关于java - 使用java rmi协议(protocol)将文本文件内容解析到客户端窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15079577/

相关文章:

command - Java jar-Archive 工具 - 设置包含内容的文件夹的路径

java - 需要帮助以更好地理解 RMI

java - 在客户端使用 RMI 远程对象的正确方法是什么?

java - 从基于 Web 的应用程序到基于桌面的应用程序的通信

带有远程参数的 Java RMI 调用失败

java - 在数据库中存储由 Java 中的 List 表示的 'large' 数据的最佳实践是什么?

java - 为 Spring-Social 实现记住我

java - 使用 RMI 传递对象引用

java - 我想使用 xpath 读取 xml 并获取 java 中父级的计数

java - 如何使用 Netbeans 包含文本文件