简体   繁体   中英

Multiple messages same PrintWriter

I'm using java and sockets to comunicate a client/server application.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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