Kristoffer Lindvall
Kristoffer Lindvall

Reputation: 2682

Which network interface on local host is connected to host B

Let's say my computer A is connected to multiple networks. On one of these networks is another host, B.

Is it possible, through C# from host A, to determine how host B would reach host A? The solution must handle both IPv4 and IPv6. It doesn't need to handle NAT.

One could say I want to figure out which one of host A's network interfaces is connected to host B.

Upvotes: 0

Views: 422

Answers (3)

Mubashir Khan
Mubashir Khan

Reputation: 1494

your catch is How to detemine which network interface (ip address) will be used to send a packet to a specific ip address?

you can find pinvoke for GetBestAddress . i am unable to find some pinvoke example for GetBestAddressEx that supports IPv6

Upvotes: 1

bmenrigh
bmenrigh

Reputation: 81

It is never possible to tell how another host will reach you because it is up to that host's route table.

However, usually routes are symmetric which means a remote host will reach you the same way you will reach that remote host. You can examine the route table. On Windows that command would be "route -n". This is probably exposed via some API too.

If host A and B are already connected, that connection will show up in netstat.

If you control the code that has the connected socket then you just need to get the peer. Sorry I don't have the C# code to do that.

Upvotes: 1

Related Questions