2

I have a desktop that has two Ethernet ports and each port connects to a different network(network A, network B).

I have a C# application that needs to run on this desktop and needs to access the net. The net should only be accessible from Network A.

The application seems to randomly select a network to try and access the net. How can I get the application to look for and only connect through Network A?

Edit: just to add more info. I need to connect to the net via network A as I need to submit a httpWebRequest and the receiver will only accept info from Network A.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
M-Corp
  • 137
  • 1
  • 3
  • 15
  • 7
    Sounds rather like a network configuration problem than a programming one. Once your routes are set up correctly the application should not need to bother with this. https://serverfault.com/ should be the right place to ask for help. – Markus Deibel Sep 16 '19 at 09:35
  • Here you have documentation that might help you https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.connect?view=netframework-4.8 – Pedro Brito Sep 16 '19 at 09:35
  • Just to clarify: is/are the intended destination host(s) reachable from both network interfaces? – T2PS Sep 16 '19 at 09:35
  • See: https://stackoverflow.com/a/4547269/11981207 – Kei Sep 16 '19 at 09:35
  • @T2PS The intended dest is only reachable through network A. so if the app tries to connect via network B it will fail. – M-Corp Sep 16 '19 at 09:42
  • 1
    The mask determine the network. You should have on the network that goes towards the internet (primary) a mask of 0.0.0.0 which is the default. Then set mask to secondary to cover the secondary network only. Use 255.0.0.0,or 255.255.0.0, or 255.255.255.0 – jdweng Sep 16 '19 at 10:01
  • Check this [Specify route to an interface in Windows cmd](https://serverfault.com/questions/818169/specify-route-to-an-interface-in-windows-cmd) – shingo Sep 16 '19 at 11:23
  • Hi all. Thanks for the answers. I used the answer @jdweng provided and it worked. – M-Corp Sep 16 '19 at 15:47

2 Answers2

2

The mask determine the network according to IP Routing Protocol. You should have on the network, that goes towards the internet (primary), a mask of 0.0.0.0 which is the default. Then set mask on secondary to cover the secondary network only. Use 255.0.0.0,or 255.255.0.0, or 255.255.255.0

jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Assuming a legitimate scenario where both networks A and B are equally valid for raw internet access (say, a device with both a wifi and wired connection that are weighted equally), you can control this using routes.

Routes are not chosen by code in your application. Rather, they are part of the network configuration on the computer. You may feel tempted to use application code to alter the network configuration, but this is almost always a very bad idea.

To set the correct route in your network configuration, you need to know the IP address of your service (this kinda sucks, since often you only start out with a host name, and the IP address might even be dynamic in the case of cloud services). You must also know the network address for connection A. The network address is different from the gateway IP address, and typically ends with a 0 (though it is possible to construct network subnets with different network addresses).

Once you add the route, connections on the computer targeting your service's address will see this new route is more specific than the default gateways at A or B, and therefore always choose it.

A common use for routes is forcing certain traffic to pass over a special connection, such as a VPN.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • This will only work if the submask of the interface is correct. The routes sub divides the mask of the interface into smaller pieces. – jdweng Sep 16 '19 at 19:46