我是 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:blalalala?
1:A
2:b
3:c
输入:a
请选择正确答案
Q2:blalalala?
1:A
2:b
3:c
输入:b
....
最终得分 2 出可能 3 Q
再见!