I have a multi-homed machine and need to answer this question:
Given an IP address of the remote machine, which local interface is appropriate to use for communication.
This needs to be done in C#. I can do this query using Win32 Socket and SIO_ROUTING_INTERFACE_QUERY but looking around in the .net framework documentation I haven't found an equivalent for it.
Someone was nice enough to writez the code, see https://searchcode.com/codesearch/view/7464800/
which is used like (example!):
Please note that although one is specifying an
IpEndPoint
with a Port, the port is irrelevant. Also, the returnedIpEndPoint.Port
is always0
.I didn't know about this, so just had a look in the Visual Studio Object browser, and it looks like you can do this from the
System.Net.Sockets
namespace.In that namespace is a
Socket
class which contains a methodIOControl
. One of the overloads for this method takes anIOControlCode
(enum in the same namespace) which contains an entry for `RoutingInterfaceQuery'.I'll try and put some code together as an example now.