Question

I generate an ByteBuffer object:

(4 bytes with 00)+(4 bytes with gson-data-length)+(some bytes with GSON - String format).

How can I send this data to my server part? Do i need to use any Encoders in my code? Or it can work without it?

public class ClientSocketInitializer extends ChannelInitializer<SocketChannel> {

    private static final ObjectEncoder ENCODER = new ObjectEncoder();

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //pipeline.addLast("encoder", ENCODER);
        pipeline.addLast("handler", new ClientSocketHandler());
    }
}

In this variant connection from client to server are present, but there is no transmitted data (0 bytes in/out) If i uncomment commented line then there is a data traffic. But i don't need to use any Encoders. I have a ready to send ByteBuffer. Please help me with this problem.

Was it helpful?

Solution

Just use:

channel.writeAndFlush(Unpooled.wrappedBuffer(yourByteBuffer));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top