BOOST ASIO: Why don't I get “bind: Address already in use” in Windows (but do get it in Linux)?

最后都变了- 提交于 2019-12-23 01:12:10

问题


Trying to bind an already bound TCP port should raise exception ("bind: Address already in use" exception).

It does happen under Linux. But under Windows , no exception is raised..

Could it be because under windows it tries to bind to ANY interface? but in Linux it tries to bind to ALL of them and raise an exception if not all of them are bound ?

Here is the code snippet:

    try {
        tcp::endpoint endpoint(tcp::v4(), 8081);
        tcp::acceptor acceptor(io_service);
        acceptor.open(endpoint.protocol());
        acceptor.set_option(tcp::acceptor::reuse_address(true));
        acceptor.set_option(tcp::acceptor::enable_connection_aborted(true));
        acceptor.bind(endpoint);
        acceptor.listen(1024)
    catch(std::exception &e) {
         cout << e.what() << endl;
    }

回答1:


In windows, the option tcp::acceptor::reuse_address is equivalent to calling setsockopt and specifying SO_REUSEADDR. This specifically allows multiple sockets to be bound to an address even if it is in use. See the MSDN documentation here.

There's a corresponding option in Win32 (SO_EXCLUSIVEADDRUSE) which is documented here. This post details some potential drawbacks with simply using SO_REUSEADDR on Win32 platforms.



来源:https://stackoverflow.com/questions/7164879/boost-asio-why-dont-i-get-bind-address-already-in-use-in-windows-but-do-ge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!