M-Corp
M-Corp

Reputation: 138

How can I connect to the net through a specific IP if my pc has multiple IP?

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.

Upvotes: 2

Views: 202

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415665

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.

Upvotes: 0

jdweng
jdweng

Reputation: 34421

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

Upvotes: 2

Related Questions