Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boostraping UDP Server for Netty 4.0?

Tags:

netty

Can someone bootstrap me with a UDP Server for Netty 4.0? i see plenty of 3.x examples, but no sign of 4.x even at netty source example. (note I am very new to Netty )

Basically, it is the example at https://netty.io/Documentation/New+and+Noteworthy#HNewbootstrapAPI but for UDP instead. Help is much appreciated

like image 666
Dan Tran Avatar asked Nov 12 '12 22:11

Dan Tran


People also ask

Is there a connectionless Bootstrap for Netty 4?

For Netty 3.x there was the ConnectionlessBootstrap shown here but for 4.0 I couldn't find something similar. Show activity on this post. From what I found out: Bootstrap is the correct entry point for UDP Servers in Netty 4.x And there is no need for NioServerDatagramChannel, because UDP servers open one channel for all clients.

Where can I download the latest version of netty?

The latest version of Netty is available in the project download page. To download the right version of JDK, please refer to your preferred JDK vendor's web site. As you read, you might have more questions about the classes introduced in this chapter.

Why Bootstrap is required to run on UDP?

Because UDP has one channel for all clients it makes sense that only the Bootstrap is requried. All clients bind to the same channel. Thanks for contributing an answer to Stack Overflow!

How do I test if my Netty server works?

You've just finished your first server on top of Netty. Now that we have written our first server, we need to test if it really works. The easiest way to test it is to use the telnet command. For example, you could enter telnet localhost 8080 in the command line and type something. However, can we say that the server is working fine?


1 Answers

Package netty-example includes classes QuoteOfTheMomentServer, QuoteOfTheMomentServerHandler, QuoteOfTheMomentClient and QuoteOfTheMomentClientHandler. These demonstrate how to create a simple UDP server.

I am pasting the code as it exists in Netty 4.1.24. I suggest to find these classes for the version of Netty that you are using.

QuoteOfTheMomentServer:

public final class QuoteOfTheMomentServer {

private static final int PORT = Integer.parseInt(System.getProperty("port", "7686"));

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(NioDatagramChannel.class)
         .option(ChannelOption.SO_BROADCAST, true)
         .handler(new QuoteOfTheMomentServerHandler());

        b.bind(PORT).sync().channel().closeFuture().await();
    } finally {
        group.shutdownGracefully();
    }
}
}

QuoteOfTheMomentServerHandler:

public class QuoteOfTheMomentServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

private static final Random random = new Random();

// Quotes from Mohandas K. Gandhi:
private static final String[] quotes = {
    "Where there is love there is life.",
    "First they ignore you, then they laugh at you, then they fight you, then you win.",
    "Be the change you want to see in the world.",
    "The weak can never forgive. Forgiveness is the attribute of the strong.",
};

private static String nextQuote() {
    int quoteId;
    synchronized (random) {
        quoteId = random.nextInt(quotes.length);
    }
    return quotes[quoteId];
}

@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
    System.err.println(packet);
    if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) {
        ctx.write(new DatagramPacket(
                Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8), packet.sender()));
    }
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.flush();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    // We don't close the channel because we can keep serving requests.
}
}
like image 166
Aleksandr Dubinsky Avatar answered Sep 17 '22 13:09

Aleksandr Dubinsky