Combining Shtééf's anwser to How does a socket know which network interface controller to use? and the MSDN Socket reference gives the following:
Suppose the PC has two interfaces:
- Wifi: 192.168.22.37
- Ethernet: 192.168.1.83
Open a socket as follows:
`
using System.Net.Sockets;
using System.Net;
void Main()
{
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int anyPort = 0;
EndPoint localWifiEP = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 22, 37 }), anyPort);
EndPoint localEthernetEP = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, 82 }), anyPort);
clientSock.Bind(localWifiEP);
// Edit endpoint to connect to an other web-api
// EndPoint webApiServiceEP = new DnsEndPoint("www.myAwsomeWebApi.org", port: 80);
EndPoint webApiServiceEP = new DnsEndPoint("www.google.com", port: 80);
clientSock.Connect(webApiServiceEP);
clientSock.Close();
}
NOTE: Using a Socket like this is somewhat low level. I could not find how to easily use Sockets —bound to a local endpoint— with higher level facilities such as HttpClient or the WCF NetHttpBinding.
For the latter you could look at How to use socket based client with WCF (net.tcp) service? for pointers how to implement your own transport.