How to set local endpoint when using boost asio udp socket

I have 3 network interfaces on a PC, and I want to make sure that when I do udp socket send, it is sent through a specific network interface (I have an IP address to use when sending data).

Here is the code.

udp::socket send_socket(io_service,boost::asio::ip::udp::v6()); udp::endpoint local_end_point(boost::asio::ip::address::from_string(LocalIpAddress),1111); // send_socket.bind(local_end_point); send_socket.send_to(const_buffer,senderEndpoint,0,error); 

The above code works, but I do not control through which network interface the data will be sent. If I uncomment the send_socket.bind line, I stop receiving any data at the other end.

Is there any other way to bind a socket to a specific network interface?

+4
source share
1 answer

The bind() function associates your socket with a specific network interface for sending and receiving. If you stop receiving data on the other end, it is likely because the other end is not routed through the network adapter that you specified in the bind call.

+4
source

All Articles