简体   繁体   中英

Apache Mina - Multiple small write to client

I built a tcp server based on apache mina 2.0.4, and have some problems writing back to the client.

We have some tcp clients that can handle only one message at a time and with a buffer size of 256 bytes max. When I send 2+ messages (< 256 bytes) to the client, they arrive in one or two big blocks that the client can't handle, instead of 2+ separated messages. I tried to set sessionConfig.setTcpNoDelay(true/false); with no success, as well as sessionConfig.setSendBufferSize( 256 ); .

In the message response encoder I also tried to flush the output:

int capacity = 256;
IoBuffer buffer = IoBuffer.allocate(capacity, false);
buffer.setAutoExpand(false);
buffer.setAutoShrink(true);
buffer.putShort(type);
buffer.putShort(length);
buffer.put(gmtpMsg.getMessage().getBytes());
buffer.flip();
out.write(buffer);
out.flush();

And in the thread responsible to send the messages, I tried to wait for the message to be written

for (Entry<Long, OutgoingMessage> outgoingMsg : outgoingMsgs.entrySet()) {
      WriteFuture future = session.write(outgoingMsg.getValue());
      future.awaitUninterruptibly();
}

All this fails miserably, and the only solution working is a ridiculous 500 msec sleep between the session write, which is hardly acceptable. Anyone see what I am doing wrong?

After reading a bit more on the tcp protocol and specially https://stackoverflow.com/a/6614586/1280034 , it is clear that the problem is on the client side, not handling the packets correctly.

Since we can't rebuilt the clients, my only solution is to delay each outgoing messages by approx 500ms. To do so I created an extra queue in charge of the writting to clients in order to let the server continue its normal job.

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