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

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

问题描述

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

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

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

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

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

解决方案

此答案参考cdhowie的答案,后者链接了一个文档,该文档指出SO_REUSEPORT将具有我想要实现的效果.

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

Boost :: Asio仅在OS等于BSD或MacOSX时才设置此选项.相应的代码包含在文件boost/asio/detail/reactive_socket_service.hpp中(Boost版本1.40,在较新的版本中,该代码已移至其他文件中).
我想知道为什么Asio没有为Linux和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

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

我不知道Windows是否存在此选项,但是通过将portable定义为也是在Linux上运行的软件的属性,这意味着SO_REUSEPORT是特定于操作系统的,因此我的问题没有可移植的解决方案

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

我会将此答案标记为已接受(尽管会因接受自己的答案而感到不好),因为它指出了为什么尝试在便携式软件中使用SO_REUSEPORT的方法会失败.

I have a client which sends data via UDP-broadcast. (To let's say 127.0.0.255: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?

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

解决方案

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.

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

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.

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

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

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.

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.

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天全站免登陆