What is the difference between writing and writing using flash?

I recently experimented with netty. I ran into the following problem:

ctx.channel().write(new TextWebSocketFrame("hello"))didnot not welcome on the client side, but ctx.channel().writeAndFlush(new TextWebSocketFrame("hello"))did.

What is the difference between the two? ctx- it is ChannelHandlerContext.

+4
source share
3 answers

According to the document

http://netty.io/4.0/api/io/netty/channel/Channel.html

  channel.writeAndFlush(msg); 

is a shortcut for

  channel.write(msg);
  channel.flush();

And flush()flushes (writes) all pending buffers.

+3
source

, , , , , , , . , , flush(), , . , , , , flush() , , . writeAndFlush , , , .

+1

, , Netty write() flush() flushes. , BufferedOutputStream.

So the question is: when to flush()? The answer is to complete the sending of the request or response if you are not going to close the channel. For example, in the client, deserves all the request cards, flash, and then reads the answer. On the server, send all parts of the response and flash.

0
source

All Articles