I have run into the issue of Windows sending out messages from the wrong network adapter when there is more than one possible network adapters on the computer. Specifically, I have a computer connected to two different subnetworks, one through the WiFi adapter (Subnet 1) and the other through ethernet (Subnet 2).
I need to specify which adapter is used to send messages to other devices on the network. Specifically, although the device CAN send messages through the corresponding adapter ie. a message to Subnet 1 through the WiFi adapter or to Subnet 2 through ethernet, I need to specify which adapter the messages are sent through. For example, if I specify that I want the application to run through ethernet (subnet 2), I need the program to send messages to Subnet 1 through the ethernet adapter DESPITE the existence of the WiFI adapter connected to Subnet 1.
It appears that this is related to the operating system's routing table. The only solution I've found is disabling the unwanted network adapter, but I would prefer to be able to manually select the correct one. I've come accross SO_BINDTODEVICE for Linux, but have not been able to find a Windows equivalent.
For example: with two network adapters
Network Adapter (A). IP address 192.168.2.100
Network Adapter (B), IP address 192.168.3.100
I want to bind to network Adapter B, and send a message from Adapter B to a device with the IP address of 192.168.2.50.
My specific use-case is a program written in C++ using winsock2.
My questions is:
- Is there a method to manually select / force Windows to send messages through a specific network adapter
- If so, can this be done internally in a program?
In my research, I have found the following stack overflow links describing a similar issue.
bind()the socket to the desired interface IP before connecting/sending. Any claims that bind doesn't work are wrong.