Simple 'byte' server

"이희승 (Trustin Lee)" trustin at gmail.com
Wed May 4 02:53:11 EDT 2011


No.  A write operation in Netty is asynchronous, so writing 10000 
messages will make them put into Channel's write request queue, 
resulting unnecessarily large memory consumption.  You'll have to write 
relatively small number of messages and write again once they are all 
sent:

int counter;

void channelConnected(...) {
    sendMessages(...);
}

void sendMessages(...) {
    ChannelFuure f = null;
    for (; counter < 10000; counter ++) {
        f = channel.write();
    }

    if (f == null || counter >= 10000) {
        return; // All sent
    }

    f.addListener(new ChannelFutureListener() {
        void operationComplete(ChannelFuture f) {
            if (f.isSuccess()) {
                sendMessages(...);
            }
        });
}

Please note that the code above is an improvised example which might 
not work out of the box, but you'll get the idea.

HTH

On Fri 18 Mar 2011 06:30:34 AM KST, Marcus wrote:
> I'm trying to use Netty to make a simple network between servers. The
> protocol isn't defined in a nice easy way that would make pipelining
> worthwhile so I'd like to be able to send and receive just bytes. This way I
> can interpret the received messages however I need.
> 
> I've got a server working that does what I would like, but my use of buffers
> seems haphazard. I'd like to be able to 'push' bytes to a buffer and receive
> them on the other server before I've finished sending a whole message. I
> know my code is wrong so I don't see much point in posting it other than to
> confuse. My explanation seems simple enough to describe the aim.
> 
> Server:
> 
> channelConnected:
>     loop 10000 times
>          send a string that contains the loop number
>          (these messages should start being received by the client before
> the loop finishes)
>      Send a final string
> 
> Make sense? 
> 
> --
> View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Simple-byte-server-tp6182595p6182595.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users


More information about the netty-users mailing list