本文介绍了Netty UDP 服务器引导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现所有使用 Netty 4.0 的 TCP 服务器实现都使用了 ServerBootstrap 实例.

All TCP Server implementations with Netty 4.0 I have found use a ServerBootstrap instance.

服务器和客户端之间最大也是唯一的区别Netty 是不同的 Bootstrap 和 Channel 实现是用过的.请看下面的代码:

netty 文档 谈论 TCP 客户端.

netty docu talking about a TCP Client.

但是对于 UDP,这句话似乎无关紧要?!我找到了仅使用 Bootstrap 的 UDP 服务器的示例:12, 3

But for UDP this quote seems irrelevant?! I found examples for UDP Servers that use just Bootstrap like: 1, 2, 3

我对此很困惑,所以我的两个问题是:

I am pretty confused by that, so my two questions are:

  • Bootstrap 类是实现 UDP 服务器的正确入口点吗? 或者我可以使用 ServerBootstrap 吗?
  • 为什么 TCP 有 NioServerSocketChannel.class 和 NioSocketChannel.class,而 UDP 只有一个 NioDatagramChannel.class(没有 NioServerDatagramChannel.class)?
  • Is the Bootstrap class a correct entry point to implement a UDP Server? or may I use ServerBootstrap for it?
  • Why is there a NioServerSocketChannel.class and NioSocketChannel.class for TCP, but only one NioDatagramChannel.class (and no NioServerDatagramChannel.class) for UDP?

对于 Netty 3.x,显示了 ConnectionlessBootstrap here 但是对于 4.0 我找不到类似的东西.

For Netty 3.x there was the ConnectionlessBootstrap shown here but for 4.0 I couldn't find something similar.

推荐答案

我发现:Bootstrap 是 Netty 4.x 中 UDP 服务器的正确入口点

From what I found out: Bootstrap is the correct entry point for UDP Servers in Netty 4.x

并且不需要 NioServerDatagramChannel,因为 UDP 服务器为所有客户端打开一个通道.

And there is no need for NioServerDatagramChannel, because UDP servers open one channel for all clients.

  • ServerBootstrap 允许许多客户端通过其通道进行连接.因此 TCP 有一个专用的 ServerSocketChannel.
  • Bootstrap 用于为单个连接创建通道.因为 UDP 对所有客户端都有一个通道,所以只需要 Bootstrap 是有道理的.所有客户端都绑定到同一个频道.

这篇关于Netty UDP 服务器引导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 15:06