使用java rmi协议将文本文件内容解析到客户端窗口?
我是RMI技术新手,在用户连接到RMI服务器后,一直在努力将文件内容解析到客户端命令窗口 问题是我可以读取文件,但它只读取第一行,而不读取文件的其余内容。有人可以查看我的代码,看看我哪里错了 接口 远程对象 RMI服务器 RMI客户端 当前结果 预期结果 客户窗口上每次应出现一个问题,当他/她回答第一个问题时,出现第二个问题,然后出现第三个问题,以及回答正确问题的最终分数 例如,在客户端窗口:使用java rmi协议将文本文件内容解析到客户端窗口?,java,rmi,Java,Rmi,我是RMI技术新手,在用户连接到RMI服务器后,一直在努力将文件内容解析到客户端命令窗口 问题是我可以读取文件,但它只读取第一行,而不读取文件的其余内容。有人可以查看我的代码,看看我哪里错了 接口 远程对象 RMI服务器 RMI客户端 当前结果 预期结果 客户窗口上每次应出现一个问题,当他/她回答第一个问题时,出现第二个问题,然后出现第三个问题,以及回答正确问题的最终分数 例如,在客户端窗口: 请选择正确答案 问题1:布拉拉拉? 1:A 2:b 3:c 输入:a 请选择正确答案 问题2:布拉拉拉
请选择正确答案
问题1:布拉拉拉?
1:A
2:b
3:c
输入:a
请选择正确答案
问题2:布拉拉拉?
1:A
2:b
3:c
输入:b
....
最终得分2分,可能3分
再见
您的while循环中有一个返回-确保只有一个迭代将以这种方式处理。。。 试试这个:
String content = "";
while(true){
String line = br.readLine();
if(line == null) break;
content += line;
}
return content;
您在while循环中有一个返回-确保只有一个迭代将以这种方式处理。。。 试试这个:
String content = "";
while(true){
String line = br.readLine();
if(line == null) break;
content += line;
}
return content;
@塞巴斯蒂安-谢谢,你知道如何一次读一个问题吗?只要新读的一行不是以“Q”开头就可以。但我通常建议使用数据库来实现这些目的。。。(如果我的答案对你有用,你能接受这个答案吗?Thx!)@Sebatian-谢谢你,你知道如何一次阅读一个问题吗?只要新阅读的一行不是以“Q”开头就可以阅读。但我通常建议使用数据库来实现这些目的。。。(如果我的答案对你有用,你能接受这个答案吗?谢谢!)
/**
* 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();
}
}
}
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:
String content = "";
while(true){
String line = br.readLine();
if(line == null) break;
content += line;
}
return content;