gpt4 book ai didi

java socket收不到数据

转载 作者:行者123 更新时间:2023-12-03 20:20:59 24 4
gpt4 key购买 nike

我必须编写向客户端请求问题并接收答案的服务器应用程序。这是我的客户端代码:

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;

客户端成功发送并稍后接收到问题,但服务器从未得到答案(读取临时变量时无限循环)。有什么问题?

最佳答案

Your calling out.print("a1"); on the client, but reading a line on the server using in.readLine(). Shouldn't you be writing out using println() on the client, else the server never gets to the end of the line? – CodeChimp Nov 21 at 21:07

感谢 CodeChimp

关于java socket收不到数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com