Let two UDP-servers listen on the same port?

前端 未结 3 1258
你的背包
你的背包 2020-12-06 00:56

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

3条回答
  •  一整个雨季
    2020-12-06 01:22

    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.

提交回复
热议问题