Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose one of many Internet connections for an application

I have a computer with a few different internet connections. LAN, WLAN, WiFi or 3G. All of these are active and the machine can use any of them.

Now I want to tell my application to use one of the available connections. For example I want to tell my application to use only WiFi while other software might use something else.

In my c# application I use classes like HttpWebRequest and HttpWebResponse.

Is this even possible?

like image 343
TalkingCode Avatar asked Dec 28 '10 15:12

TalkingCode


People also ask

What are the 4 internet connections?

Internet Connection Types: WiFi, Broadband, DSL, Cable.

How do I use multiple internet connections?

Start by connecting your computer to your modem or router via an ethernet cable. Once done, connect to a different internet provider via Wi-Fi. You can connect to a third provider by connecting a USB cellular data adapter to your computer.

How many types of internet connection are there?

There are mainly two types of internet. The age-old dial-up internet connection, which has become almost irrelevant today, and broadband. Broadband covers all the different types of internet connection types that we will be discussing and includes DSL, Cable, Fiber Optic, and Satellite.


1 Answers

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);
        }
    }
}
like image 107
sisve Avatar answered Oct 18 '22 18:10

sisve