Q:

Can't send message on specified address with Netty UDP and SimpleChannelInboundHandler

I have Netty server which listens some port and send response on incoming messages. Server implementation:
@Override
public void run() {
    final NioEventLoopGroup group = new NioEventLoopGroup();
    try {
        log.info("Started listening logs ...");
        final Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<NioDatagramChannel>() {
                    @Override
                    public void initChannel(final NioDatagramChannel nioDatagramChannel) {
                        ChannelPipeline channelPipeline = nioDatagramChannel.pipeline();
                        channelPipeline.addLast(encryptedPacketHandlerChain);
                    }
                });

        // Bind and start to accept incoming connections.
        InetAddress address = InetAddress.getByName("localhost");

        bootstrap.bind(address, LOG_PORT).sync().channel().closeFuture().await();
    } catch (Exception e) {
        log.error("Unexpected error happened", e);
    } finally {
        System.out.print("Stop server");
    }
}

EncryptedPacketHandlerChain implementation:
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
    final String hostAddress = datagramPacket.sender().getAddress().getHostAddress();
    final ByteBuf byteBuffer = datagramPacket.content();
    final int receivedPacketLength = byteBuffer.readableBytes();
    final byte[] receivedPacket = new byte[receivedPacketLength];
    byteBuffer.readBytes(receivedPacket);

    EncryptedPacket encryptedPacket = EncryptedPacket.fromBytes(receivedPacket);

    byte[] response = EncryptedPacket.toBytes(handle(encryptedPacket, hostAddress));

    System.out.println("Response\n" + Arrays.toString(response));

    ByteBuf data = ctx.alloc().buffer(response.length);
    data.writeBytes(response);

    InetSocketAddress socketAddress = datagramPacket.sender();
    ctx.write(new DatagramPacket(data, socketAddress));
    ctx.flush();
    
    System.out.println("Send to " + socketAddress);
    }

It is quite simple implementation which I read in official netty docs. But I don't get response from Netty server after I send request from my local sender on 127.0.0.2 host address.
All works when I send response with java.net DatagramSocket, but I need to respond from the same socket i.e. Netty to the sender address and port. What is wrong in my code?
UPD: I need to send response from netty on specified address and specified port, which I can get from income datagramPacket.

A:

You should check if the write failed and if so why:
...
ctx.writeAndFlush(new DatagramPacket(data, socketAdress)).addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture f) {
        if (!f.isSuccess()) {
            f.cause().printStackTrace();
        }
    }  
 });

