0

I am writing server-client app using Netty 4.1.9.

Recently I successfully made communication using ByteBuf. Then tried with String and succeed again.

But with last step I wanted to send custom Object and there is my problem:

Send failed: java.lang.UnsupportedOperationException: unsupported message type: GIdPacket (expected: ByteBuf, FileRegion)

Some code parts

Server:

ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
    .channel(NioServerSocketChannel.class)
    .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
             ch.pipeline().addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
             ch.pipeline().addLast(new ObjectEncoder());
             ch.pipeline().addLast(new ServerHandler());
             ch.pipeline().addLast(loggingHandler);
         }
     })
    .option(ChannelOption.SO_BACKLOG, 128)
    .childOption(ChannelOption.SO_KEEPALIVE, true)
    .option(ChannelOption.AUTO_READ, true);

ChannelFuture f = b.bind(port).sync();

ServerHandler:

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception
{
    System.out.println("CHANNEL >> channelActive fired");
    clientData = new ClientData(Server.newId(), ctx.channel());
    Server.clients.put(clientData.id, clientData);

    GIdPacket packet = new GIdPacket(/*some init atr*/);

    ChannelFuture cf = ctx.writeAndFlush(packet);
    if (!cf.isSuccess()) {
        System.out.println("Send failed: " + cf.cause());
    }

    Log.out(packet.toString());
}

So, I tried many different options. I spent much time on that and I didn't found solution. Any help appreciated.

1 Answer 1

2

Looks like GIdPacket doesn't implement Serializable interface. According to the docs, ObjectEncoder supports only serializable objects: class ObjectEncoder extends MessageToByteEncoder<Serializable>.

By the way, you're trying to check channelFuture.isSuccess() immediately after execution writeAndFlush(..). Because Netty is completly asynchronous, this test will fail because channelFuture is neither successful nor failed yet. You must wait until CF is completed by calling cf.await() or cf.awaitUninterruptibly().

Sign up to request clarification or add additional context in comments.

2 Comments

So like you said, I made my Objects serializable and it works now. Grats!
Never call cf.await* in the EventLoop thread. You should register a listener to the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.