我已经编写了一个 EchoServer,如果数据已相应发送,它会以 String ack 响应。
我的客户端看起来像这样......为了从服务器接收确认应答,“echoSocket”将接收到的数据放入我的 ObjectInputStream 中。只有当我将这些部分注释掉时,客户端才能工作
echoSocket = new Socket(server_name, tcp_port);
System.out.println(" *** Connected to " + server_name + " ***");
System.out.println("Press Enter to send your message.");
out = new ObjectOutputStream(echoSocket.getOutputStream());
in = new ObjectInputStream(echoSocket.getInputStream());
out.flush();
String message = System.console().readLine();
while(!message.equals("quit")) {
// problem
if (in.readObject().equals(ack))
System.out.println("ACKed");
in.close();
// problem ends
out.flush();
out.writeObject(message);
System.out.println("Sending: " + message);
message = System.console().readLine();
out.flush();
}
有人知道为什么它不发送我的字符串吗?
谢谢, 马吕斯
最佳答案
在套接字上使用对象流并不是一个值得推荐的想法。您可能会考虑完整的 Web 服务或 RMI。
如果您读入缓冲区并确保在尝试使用对象流反序列化之前拥有整个业务,它可能会更好。
关于java - 对象输入流和对象输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1780281/