为Netty 4.0启动UDP服务器?

为Netty 4.0启动UDP服务器?,netty,Netty,有人可以为我引导一个适用于Netty 4.0的UDP服务器吗?我看到了大量的3.x示例,但即使在netty源代码示例中也没有看到4.x的迹象。(注:我对Netty很陌生) 基本上,这是at的示例,但用于UDP。非常感谢您的帮助软件包网络示例包括类和。这些演示了如何创建一个简单的UDP服务器 我正在粘贴Netty 4.1.24中的代码。我建议为您正在使用的Netty版本找到这些类 QuoteOfTheMomentServer: public final class QuoteOfTheMoment

有人可以为我引导一个适用于Netty 4.0的UDP服务器吗?我看到了大量的3.x示例,但即使在netty源代码示例中也没有看到4.x的迹象。(注:我对Netty很陌生)


基本上,这是at的示例,但用于UDP。非常感谢您的帮助

软件包
网络示例
包括类和。这些演示了如何创建一个简单的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.
}
}
公共类QuoteOfTheMomentServerHandler扩展了SimpleChannelInboundHandler{
私有静态最终随机=新随机();
//莫汉达斯·甘地的名言:
私有静态最终字符串[]引号={
“哪里有爱,哪里就有生命。”,
“首先他们忽视你,然后他们嘲笑你,然后他们和你打架,然后你赢了。”,
“成为你想在世界上看到的改变。”,
“弱者永远不会原谅。原谅是强者的属性。”,
};
私有静态字符串nextQuote(){
int quoteId;
同步(随机){
quoteId=random.nextInt(quotes.length);
}
返回引号[quoteId];
}
@凌驾
public void channelRead0(ChannelHandlerContext ctx,DatagramPacket数据包)引发异常{
系统错误打印项次(数据包);
if(“QOTM?”.equals(packet.content().toString(CharsetUtil.UTF_8))){
ctx.write(新数据包)(
copiedBuffer(“QOTM:+nextQuote(),CharsetUtil.UTF_8),packet.sender());
}
}
@凌驾
公共无效channelReadComplete(ChannelHandlerContext ctx){
ctx.flush();
}
@凌驾
公共无效例外情况(ChannelHandlerContext ctx,可丢弃原因){
cause.printStackTrace();
//我们不会关闭频道,因为我们可以继续服务请求。
}
}

欢迎来到StackOverflow。请阅读常见问题解答-特别是关于欢迎在此提出的问题类型。这一个可能在其他堆栈交换站点上更好。这对于SO来说是一个非常好的问题。检查此项目,看起来他们正在使用Netty 4和UDP:Hi@aleksandr dubinsky,QuoteOfTheMomentServer使用Bootstrap类,而不使用ServerBootstrap。如果使用ServerBootstrap,则不能使用示例中提到的NioDatagramChannel,因为它不从ServerChannel扩展。您能解释一下为什么这个示例使用引导类,它通常用于客户端而不是服务器上吗?我知道这个例子的行为就像一个服务器,我想这回答了我的问题。如果我错了,请纠正我。