Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/
like image 774
skimobear Avatar asked Aug 25 '10 22:08

skimobear


1 Answers

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
            );
}
like image 101
Sam Miller Avatar answered Oct 18 '22 10:10

Sam Miller