I have this mess problem where sending multiple messages (sending Linux) in quick succession to a Java (NIO) server The message should lead to the cut packet for a client and the message should be sent very fast to the problem. Here's what my code is basically doing (not the actual code, but more or less happening):
// - Setup stuff: - charset charset = charset. Name ("UTF-8"); Charset Encoder Encoder = Charset NEE Encoder (); String msg = "a very long message (let's say 20KB) ..."; // - Inside the loop to control incoming connections: - ServerSocketChannel ssc = (ServerSocketChannel) key.channel (); SocketChannel sc = ssc.accept (); Sc.configureBlocking (wrong); . Sc.socket () setTcpNoDelay (true); Sc.socket () setSendBufferSize (1024 * 1024). // - Afterwards, to send the actual message: {BitBuffer BB = encoder.nnded (charbufar.wp (msg + '\ 0')) for (int n = 0; nandt; 20; n ++); Sc.write (bb); Bb.rewind (); }
Therefore, if the packets have been sent for a long time and as soon as possible (i.e. in the loop with a delay in this way), then at the other end, something like this often comes out [Full packet 1] [full packet 2] [full packet 3] [start off packet 4] [some or all packet 5] < P> Data loss, and packets begin to move simultaneously, such as the beginning of packet 5 (in this example) in the same message as the beginning of packet 4 The breakfast. It's not just sorting, its message is running simultaneously. / P>
I think it is related to TCP buffer or "window size", or that the server here is providing more data than just OS, or network adapter, or some other data, but I can handle it How do I check for it, and prevent it from happening? If I reduce the length of the message towards the use of sc.write (), but then increase the iterations, I will still go into the same problem. It seems that there may be a problem with the amount of data in just a small amount. I do not think sc.write () is throwing any exception (I know that in the example above I am not checking, But in my tests).
I would be happy if I could programmatically check that it is not ready for much data yet, and has delayed it, and wait until it is ready . I'm also not sure that "sc.socket (). SetSendBufferSize (1024 * 1024);" It has no effect, or if I have to adjust it on the Linux side. Is there any way to actually "flush" the socket channel? As a lame solution, I can clearly send anything that is buffer at any time, which I am trying to send more than 10KB messages, for example (which is not often in my application ). But I do not know in any way (or did not wait until sending it) to force buffer to send. Thanks for any help!
There are several reasons that sc.write () will not send some or all of the return value of data and / Or the number of remaining bytes in the buffer should be checked. (Int n = 0; n & lt; 20; n ++) {ByteBuffer bb = encoder.encode CharBuffer.wrap (msg + '\ 0')) for
If (sc.write (bb)> 0 & amp; amp; bb.remaining () == 0) {// All data sent} else {// can not send all data} BBWindia (); }
Comments
Post a Comment