c# - Limit webclient to use wifi or ethernet, or vice versa -
i have connected wifi , have access internet ethernet. there anyway control webclient.downloadstring use ethernet instead of wifi or wifi instead of ethernet?
this advanced functionality abstracted away both httpwebrequest, webrequest, webclient , like. can, however, using tcpclient (using constructor taking local endpoint) or using sockets , calling socket.bind.
use bind method if need use specific local endpoint. must call bind before can call listen method. not need call bind before using connect method unless need use specific local endpoint. bind local endpoint interface want use. if local machine have ip address 192.168.0.10 wifi address, using local endpoint force sockets use interface. default unbound (really 0.0.0.0) tells network stack resolve interface automatically, want circumvent.
here's example code based on andrew's comment. note specifying 0 local endpoint port means dynamic.
using system.net; using system.net.sockets; public static class consoleapp { public static void main() { { // 192.168.20.54 local network 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 vpn, having no default gateway , unable forward // packages outside of 192.168.2.x var localendpoint = new ipendpoint(ipaddress.parse("192.168.2.49"), port: 0); var tcpclient = new tcpclient(localendpoint); // socketexception: socket operation attempted unreachable network 64.34.119.12:80 tcpclient.connect("stackoverflow.com", 80); } } }
Comments
Post a Comment