私は 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:
期待される結果
一度に 1 つの質問がクライアント ウィンドウに表示されます。最初の質問に回答すると、2 番目の質問が表示され、次に 3 番目の質問と、正しい質問に回答した場合の最終スコアが表示されます。
クライアントウィンドウの例:
正しい答えを選択してください
Q1: blalalala?
1:A
2:b
3:c
入力:a
正しい答えを選んでください
Q2: ブララララ?
1:A
2:B
3:C
入力:B
....
最終スコア 2アウト可 3Q
さようなら!