boost::asio - Clarification on binding to a specific network interface

岁酱吖の 提交于 2019-12-06 02:32:45

问题


I've been searching the net for answers but I can't seem to find a complete answer.

Scenario: I have a client API and a server. An application uses the client API to talk to the server. Both TCP and UDP are used to communicate between the client API and the server. All of this has been written using ASIO.

The client API connects to the server via TCP, then sends commands via TCP and receives responses via TCP. The client API also listens to a UDP address where it receives real-time data continuously.

The environment is a mix of machines running WIN32 and WIN64. All of the machines also have 2 network cards.

Question: I would like to be able to 'pin' my TCP and UDP connections to specific local network interfaces. I have seen some info that discusses the SO_BINDTODEVICE socket option as well as the bind function from earlier posts or other sites.

Is it possible to do this in the WIN32/64 environment? If you could shed some light on this, some examples, or sites that are helpful I would really appreciate it.

Links I've found:

  1. Using Linux, how to specify which ethernet interface data is transmitted on
  2. http://tuxology.net/2008/05/15/forcing-connections-through-a-specific-interface/

回答1:


You can bind to a specific endpoint using the appropriate tcp::ip::acceptor constructor

include <boost/asio.hpp>

int
main()
{
    using namespace boost::asio;
    io_service service;
    ip::tcp::endpoint ep(
            ip::address::from_string( "127.0.0.1" ),
            1234
            );
    ip::tcp::acceptor acceptor( 
            service,
            ep
            );
}


来源:https://stackoverflow.com/questions/3570685/boostasio-clarification-on-binding-to-a-specific-network-interface

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