简体   繁体   English

多个消息相同的PrintWriter

[英]Multiple messages same PrintWriter

I'm using java and sockets to comunicate a client/server application. 我正在使用Java和套接字来通信客户端/服务器应用程序。

I want to send a message to server like this: 我想像这样向服务器发送一条消息:

is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

pw = new PrintWriter(socket.getOutputStream());

pw.println("MESSAGE");

pw.flush();

And that worked well, but then after that I'm trying to send another message 效果很好,但是之后我尝试发送另一条消息

pw.println("SECOND MESSAGE");

pw.flush();

And the second message is not sending! 第二条消息没有发送! What can I do? 我能做什么?

Ensure that your server side is consistently reading for more input. 确保您的服务器端不断读取更多信息。 If you only have it say for example performing: 如果只有它,例如执行以下操作:
bufferedReader.readLine();
only once then this is the reason you think it isn't receiving it. 只有一次,这就是您认为它没有收到的原因。 It probably is if the connection isn't closed. 可能是连接没有关闭。

Another possibility since you said it isn't "sending" the second message, is ensure the socket connection remains open and that the reader is still open on the server side and that it wasn't closed after the first message was received. 因为您说过它不是“发送”第二条消息,所以另一种可能是确保套接字连接保持打开状态,并且阅读器在服务器端仍处于打开状态,并且在收到第一条消息后没有关闭它。

发送所有消息时,请使用flush()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM