Choose One of Many Internet Connections for an Application

Choose one of many Internet connections for an application

This is somewhat advanced functionality which is abstracted away by both HttpWebRequest, WebRequest, WebClient and the like. You can, however, do this using TcpClient (using the constructor taking a local endpoint) or using sockets and calling Socket.Bind.

Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the Listen method. You do not need to call Bind before using the Connect method unless you need to use a specific local endpoint.

Bind to a local endpoint for the interface you want to use. If your local machine have ip address 192.168.0.10 for the WiFi address, then using that a local endpoint will force sockets to use that interface. Default is unbound (really 0.0.0.0) which tells the network stack to resolve the interface automatically, which you want to circumvent.

Here's some example code based on Andrew's comment. Note that specifying 0 as local endpoint port means that it is dynamic.

using System.Net;
using System.Net.Sockets;

public static class ConsoleApp
{
public static void Main()
{
{
// 192.168.20.54 is my local network with internet accessibility
var localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.20.54"), port: 0);
var tcpClient = new TcpClient(localEndPoint);

// No exception thrown.
tcpClient.Connect("stackoverflow.com", 80);
}
{
// 192.168.2.49 is my vpn, having no default gateway and unable to forward
// packages to anything that is outside of 192.168.2.x
var localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.49"), port: 0);
var tcpClient = new TcpClient(localEndPoint);

// SocketException: A socket operation was attempted to an unreachable network 64.34.119.12:80
tcpClient.Connect("stackoverflow.com", 80);
}
}
}

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

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

Programmatically choosing the Internet connection (WiFi, mobile broadband,...)

This question Choose one of many Internet connections for an application basically answers the same problem.

You have to set the ServicePoint manually for your HttpWebRequest (which I assume you are using?). It's a bit of a hassle but totally doable. The idea behind using WebClient / HttpWebRequest is that you shouldn't have to worry about interfaces though :)

If you would like some code examples, just comment this answer with what's troubling you.

In Android, if both mobile and wifi networks are available, which one is used?

It is totally depended on device how its ROM is configured. Priority is set in some of the phones. Some of the old device which do not have priorities use the network which is latest connected.

There are many apps available to use both connection simultaneously, but the primary requirement for that is the phone must be rooted.

Sample Image

Apps are also available that notifies user to switch network when multiple connections are available.



Related Topics



Leave a reply



Submit