我必须编写服务器应用程序,从客户端请求问题并收到答案。这是我的客户代码:
clientSocket = new Socket("localhost", 1234);
System.err.println("Client started");
//get questions
ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Question q = (Question)in.readObject();
//send answer
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.print("a1");
out.flush();
和服务器代码:
//sending questions
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
List<Question> quest = Questions.getInstance().getQuestions();
out.writeObject(quest.get(0));
out.flush();
//get answer
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String temp = null;
while ((temp = in.readLine()) == null) {}
String answer = temp;
客户端成功发送并稍后收到的问题,但服务器永远不会得到答案(读取临时变量时无限循环)。有什么问题?
答案 0 :(得分:0)
你的呼唤out.print(“a1”);在客户端上,但使用in.readLine()在服务器上读取一行。你不应该在客户端上使用println()写出来,否则服务器永远不会到达行尾? - CodeChimp 11月21日21:07
感谢CodeChimp