0

Here is my problem: My computer is connected to internet with ethernet cable, and also connected to a wifi module. But when I want to send TCP to my module, it always choose ethernet by default.

I have to disconnect my cable to send TCP to my module, but I will need the two connections and know how to swap between them.

How can I choose the interface i want in c#? Can I swap between them in a single program? I already use System.Net and the Managed Wifi API.

Thank you for your answers.

Edit:

As @Someprogrammerdude and @sam suggest, i use socket.bind() with the IP address of the default gateway of my wifi to send TCP on it. But when I try to bind, there the requested address is not valid in this context error. So I checked the default gateway address (sry french) : ipconfig

We can see that my wifi default gateway is 192.168.1.2 and ethernet 192.168.1.1. I tried to put them "manually" but i have the same result.. It cannot be the port i checked myself on the module that he is well opened

Edit 2: Nvm, i had to bind 192.168.1.100, and not 192.168.1.2. Thank you for your answers, you helped me a lot.

Nazoum
  • 9
  • 3
  • You can *bind* a socket to a specific interface. – Some programmer dude Jul 18 '17 at 07:46
  • 1
    Take a look here. https://stackoverflow.com/questions/49507/controlling-which-network-card-tcp-ip-message-are-sent-on – Mauro Sampietro Jul 18 '17 at 07:50
  • @Someprogrammerdude, you can choose only address and port with binding, i would like to choose if could choose in addition wifi or ethernet – Nazoum Jul 18 '17 at 07:56
  • @sam I've already checked this answer, I tested it but it keeps choosing the ethernet while I want to send on wifi. And in this code, it doesn't seem to choose between wifi and ethernet. – Nazoum Jul 18 '17 at 07:59
  • 1
    @Nazoum you don't really choose wifi or ethernet, you choose the ip of the gateway – Mauro Sampietro Jul 18 '17 at 08:00
  • 1
    @Nazoum in the link i provided you, the first address of the list is always used (FirstOrDefault) in your case this is obviously wrong, you just have to check the list and find out which adapter in that list is the one you wanna use – Mauro Sampietro Jul 18 '17 at 08:02
  • And to get a hold of the right IP for the Wifi adapter you are after, see this answer: https://stackoverflow.com/questions/9855230/how-do-i-get-the-network-interface-and-its-right-ipv4-address. Combine the two to get your desired result. – Paul-Jan Jul 18 '17 at 08:56
  • ok it seems right but when i try to bind it says that `the requested address is not valid in its context` , but i checked with my terminal and `ipconfig` command and it seems to be the same... – Nazoum Jul 18 '17 at 09:30

1 Answers1

0

Which interface is used is decided by the local routing table. A cable interface (often) is faster, so it'll get a lower network metric and is subsequently preferred. With DHCP clients, this may also be configured from the DHCP server (if it allows manual settings).

Depending on your system, you can manipulate the interfaces' network metrics (likely need to deactivate DHCP for that) and if you'd like to prefer wireless, just reduce its metric below that of the Ethernet interface. Alternatively, you can add routes to certain services you want to be transferred via wireless to the wireless interface with a lower metric.

Zac67
  • 2,761
  • 1
  • 10
  • 21