让两个 UDP 服务器监听同一个端口? [英] Let two UDP-servers listen on the same port?

查看:105
本文介绍了让两个 UDP 服务器监听同一个端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 UDP 广播发送数据的客户端.(比方说 127.0.0.255:12345)

I have a client which sends data via UDP-broadcast. (To let's say 127.0.0.255:12345)

现在我想让多个服务器监听这些数据.要在本地机器上执行此操作,他们需要共享端口 12345 以进行侦听.

Now I want to have multiple servers listening to this data. To do so on a local machine, they need to share the port 12345 for listening.

我的问题是,如果可能的话,是否有任何缺点以及这种方法是否存在问题.

My question is, if that is possible, if there are any disadvantages and if there could be problems with this approach.

不幸的是,有一种选择会带来很多开销:
实现某种注册过程.在启动时,每个服务器都会告诉客户端它的端口.客户端然后将消息发送到每个端口(必须多次发送数据,需要实现某种握手...)
你知道更好的选择吗?

There is one alternative which unfortunately brings with a lot of overhead:
Implement some kind of registration-process. On startup, each server tells the client its port. The client then sends the messages to each port (having to send the data multiple times, some kind of handshaking needs to be implemented...)
Do you know any better alternative?

如果这很重要:
我在 Boost::Asio 中使用 C++.软件应该是可移植的(主要是Linux和Windows).

If that matters:
I'm using C++ with Boost::Asio. The software should be portable (mainly Linux and Windows).

推荐答案

这个答案参考了 cdhowie 的答案,他链接了一个文档,该文档指出 SO_REUSEPORT 会产生我想要达到的效果.

This answer is referenced to the answer of cdhowie, who linked a document which states that SO_REUSEPORT would have the effect I'm trying to achieve.

我研究了如何以及是否实施此选项,并主要关注 Boost::Asio 和 Linux.

I've researched how and if this option is implemented and focused mainly on Boost::Asio and Linux.

Boost::Asio 仅在操作系统等于 BSD 或 MacOSX 时才设置此选项.该代码包含在文件 boost/asio/detail/reactive_socket_service.hpp 中(Boost 版本 1.40,在较新的版本中,代码已移至其他文件中).
我想知道为什么 Asio 没有为 Linux 和 Windows 等平台定义这个选项.

Boost::Asio does only set this option if the OS is equal to BSD or MacOSX. The code for that is contained in the file boost/asio/detail/reactive_socket_service.hpp (Boost Version 1.40, in newer versions, the code has been moved into other files).
I've wondered why Asio does not define this option for platforms like Linux and Windows.

有几个参考资料讨论了这在 Linux 中没有实现:https://web.archive.org/web/20120315052906/http://kerneltrap.org/mailarchive/linux-netdev/2008/8/7/2851754
http://kerneltrap.org/mailarchive/linux-kernel/2010/6/23/4586155

There are several references discussing that this is not implemented in Linux: https://web.archive.org/web/20120315052906/http://kerneltrap.org/mailarchive/linux-netdev/2008/8/7/2851754
http://kerneltrap.org/mailarchive/linux-kernel/2010/6/23/4586155

还有一个补丁可以将此功能添加到内核中:https://web-beta.archive.org/web/20110807043058/http://kerneltrap.org/mailarchive/linux-netdev/2010/4/19/6274993

There also is a patch which should add this functionality to the kernel: https://web-beta.archive.org/web/20110807043058/http://kerneltrap.org/mailarchive/linux-netdev/2010/4/19/6274993

我不知道 Windows 是否存在此选项,但是通过将 portable 定义为同样在 Linux 上运行的软件的属性,这意味着 SO_REUSEPORT 是特定于操作系统的,并且没有我的问题的便携式解决方案.

I don't know if this option is existing for Windows, but by defining portable as an attribute for software which runs on Linux too, this means, that SO_REUSEPORT is OS specific and there is no portable solution for my question.

在我链接的其中一个讨论中,建议 UDP 实现一个主侦听器,然后将传入的数据提供给多个从属侦听器.

In one of the discussions I've linked it is recommended for UDP to implement a master-listener which then provides the incoming data to multiple slave-listeners.

我会将这个答案标记为已接受(尽管接受我自己的答案感觉有点糟糕),因为它指出了为什么使用 SO_REUSEPORT 的方法在尝试与便携式软件一起使用时会失败.

I will mark this answer as accepted (though feeling kind of bad by accepting my own answer), because it points out why the approach of using SO_REUSEPORT will fail when trying to use it with portable software.

这篇关于让两个 UDP 服务器监听同一个端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆