7

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/
Community
  • 1
  • 1
skimobear
  • 1,188
  • 10
  • 12
  • it wasn't obvious to me if you've tried binding your `acceptor` objects an a specific `endpoint` using one of the overloaded constructors. Have you tried that? – Sam Miller Aug 25 '10 at 22:34
  • Sam - I haven't tried the overloaded constructor. I'll try that tonight and let you know. Sounds like it should do the trick for a tcp server. I'm hoping to do this for a tcp client, udp client and udp server as well. If they are possible. Can I use the bind method on them as well? Thanks, will post the result. – skimobear Aug 26 '10 at 00:43
  • I've posted an answer below with some sample code, give it a shot and let me know. – Sam Miller Aug 26 '10 at 01:33

1 Answers1

11

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
            );
}
Sam Miller
  • 23,808
  • 4
  • 67
  • 87