【问题标题】:Boostraping UDP Server for Netty 4.0?为 Netty 4.0 提升 UDP 服务器?
【发布时间】:2012-11-01 09:46:03
【问题描述】:

有人可以使用 Netty 4.0 的 UDP 服务器引导我吗?我看到很多 3.x 示例,但即使在 netty 源示例中也没有 4.x 的迹象。 (注意我对 Netty 很陌生)

基本上,它是https://netty.io/Documentation/New+and+Noteworthy#HNewbootstrapAPI 的示例,但用于 UDP。非常感谢您的帮助

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读常见问题解答 - 特别是关于此处欢迎的问题类型。这个在其他 Stack Exchange 站点之一上可能会更好。
  • 这对 SO 来说是一个非常好的问题。
  • 检查这个项目,看起来他们正在使用 Netty 4 和 UDP:github.com/menacher/java-game-server/blob/netty4/nadron/src/…

标签: netty


【解决方案1】:

netty-example 包括类QuoteOfTheMomentServerQuoteOfTheMomentServerHandlerQuoteOfTheMomentClientQuoteOfTheMomentClientHandler。这些演示了如何创建一个简单的 UDP 服务器。

我正在粘贴 Netty 4.1.24 中存在的代码。我建议为您正在使用的 Netty 版本找到这些类。

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.
}
}

【讨论】:

  • 嗨@aleksandr-dubinsky,QuoteOfTheMomentServer 使用Bootstrap 类,它不使用ServerBootstrap。如果我使用 ServerBootstrap,那么示例中提到的 NioDatagramChannel 不能使用,因为它不是从 ServerChannel 扩展而来的。您能否澄清一下为什么该示例使用通常在客户端而不是服务器上使用的 Bootstrap 类?我知道该示例的行为类似于服务器。
  • 我认为这回答了我的问题。 stackoverflow.com/a/37681245/1756069如果我错了,请纠正我。
猜你喜欢
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
相关资源
最近更新 更多