Netty doesn't write

When trying to write with netty, the written data never ends up at the remote side, confirmed with Wireshark.

I have tried:

//Directly using writeAndFlush
channel.writeAndFlush(new Packet());

//Manually flushing
channel.write(new Packet());
channel.flush();

// Even sending bytes won't work:
channel.writeAndFlush(new byte[]{1,2,3});

No exception is caught when I wrap it in try{...}catch(Throwable e){e.printStackTrace();}

What can I do to debug this problem?


Netty is asynchronous, meaning that it won't throw exceptions when a write failed. Instead of throwing exceptions, it returns a Future<?> that will be updated when the request is done. Make sure to log any exceptions coming from this as your first debugging steps:

channel.writeAndFlush(...).addListener(new GenericFutureListener<Future<Object>>() {
    @Override
    public void operationComplete(Future<Object> future) {
        // TODO: Use proper logger in production here
        if (future.isSuccess()) {
            System.out.println("Data written succesfully");
        } else {
            System.out.println("Data failed to write:");
            future.cause().printStackTrace();
        }
    }
});

Or more simply:

channel.writeAndFlush(...).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);

After you get the root cause of the exception, there could be multiple problems:

java.lang.UnsupportedOperationException:
unsupported message type: <type> (expected: ...)

Notice: This also throws when using an ObjectEncoder, but your object does not implements Serializable

A default Netty channel can only send ByteBufs and FileRegions. You need to convert your objects to these types either by adding more handlers to the pipeline, or converting them manually to ByteBufs.

A ByteBuf is the Netty variant of a byte array, but has the potential for performance because it can be stored in the direct memory space.

The following handlers are commonly used:

To convert a String use a StringEncoder To convert a Serializable use a ObjectEncoder (warning, not compatible with normal Java object streams) To convert a byte[] use a ByteArrayEncoder

Notice: Since TCP is a stream based protocol, you usually want some form of packet sizes attached, since you may not receive exact packets that you write. See Dealing with a Stream-based Transport in the Netty wiki for more information.