Le 11/11/13 4:26 PM, jonas85 a écrit :
> Hello,
>
> I think I have a problem that's related to the TcpNoDelay option. I'm
> writing a server, which, in one function, runs through a loop, gets a string
> in each iteration and sends it to the client, basically like the following:
>
> String tempStr;
> IoSession session; 
> for(int i = 0; i < 10; i++)
> {
>     tempStr = getResultString(i);
>     session.write(tempStr);                     
> }
>
> My problem is that the server seems to wait first and does nothing, then
> sends all strings at the same time. I'm currently initializing the server
> like this:
>
> SocketAcceptor acceptor = new NioSocketAcceptor();
> acceptor.getSessionConfig().setTcpNoDelay(true);
>
> Unfortunately, this doesn't change anything. And now I'm clueless and need
> your help!

That's quite simple : the session.write() is not blocking, so your code
is just pushing messages into a queue as fast as it can (ie, way faster
than the thread in charge of sending those messages to your client can do).

If you want to be sure that the server send a new message only when the
previous message has been written into the socket, then do a
session.write() when you receive the messageSent() event for the
previous message.

You can also get the writeFuture return by the session.write() call and
wait for it to be completed before sending a new message, but that would
block a thread for quite a long time.

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com 

Reply via email to