0

I use mono to compile and run my C# console application. In that application i do several Http requests. Now i am in trouble with selecting proper network interface for app instance to send requests from.

Let's say i have eth0 and eth1 interfaces in my system. I need to force my application to use both of them in different instances. For example, when it starts first time, app chooses to use eth0 and writes its number to cache.txt. If it starts again, it chooses second interface because cache.txt says that first interface is used by another instance.

Question: In C# application for mono runtime, how can enumerate and specify (at app startup) a network interface to be used for all TCP/Http/udp requests during this instance?

Croll
  • 3,631
  • 6
  • 30
  • 63
  • For inbound TcpListeners you can bind to an IP/Port and thus that would be either your `eth0` or `eth1` IP address. For outbound, you are looking at your TCP stack's `routing table`, i.e. `route -n`. If they both have access to the same subnets/gateway/etc... then the decision is based on the routing tables's metric – SushiHangover Nov 20 '15 at 21:07
  • I am not sure what you're talking about. My primary question is what .Net/Mono classes or namespaces can help to select which interface is used by default in application for outgoing TCP/HTTP (by interfaces i actually mean IP address connected to my host and yes, available to use). – Croll Nov 20 '15 at 21:12
  • Other than that, you can control via `IPEndPoint`(s), look @ http://stackoverflow.com/questions/4547032/choose-one-of-many-internet-connections-for-an-application – SushiHangover Nov 20 '15 at 21:13
  • Now i know that i need to explicitly bind on port and address before the request (which normally is not needed to connect), but how can i tweak my Http requests to bind on specific local endpoint by default? – Croll Nov 20 '15 at 21:15
  • If the http request class that you are using do not expose the socket's IPEndPoint (or similar), than you can't... Classes like `tcpClient` do expose it, but http ones...(?) – SushiHangover Nov 20 '15 at 21:18
  • Code in answer http://stackoverflow.com/a/5157593/3367446 looks like what i need. The question does it work as expected. I will test later but possibly it is not correct. – Croll Nov 20 '15 at 21:22
  • Cool, totally forgot about the callback... I'm interested now in knowing if it works correctly with the Mono framework on Linux... ;-) – SushiHangover Nov 20 '15 at 21:25

0 Answers0