<p>The system I'm currently working on consists of a controller PC running XP with .Net 2 connected to a set of embedded systems. All these components communicate with each other over an ethernet network. I'm currently using TcpClient.Connect on the XP computer to open a connection to the embedded systems to send TCP/IP messages. </p>

<p>I now have to connect the XP computer to an external network to send processing data to, so there are now two network cards on the XP computer. However, the messages sent to the external network mustn't appear on the network connecting the embedded systems together (don't want to consume the bandwidth) and the messages to the embedded systems mustn't appear on the external network.</p>

<p>So, the assertion I'm making is that messages sent to a defined IP address are sent out on both network cards when using the TcpClient.Connect method.</p>

<p>How do I specify which physical network card messages are sent via, ideally using the .Net networking API. If no such method exists in .Net, then I can always P/Invoke the Win32 API.</p>

<p>Skizz</p>

[{'answer_id': 49518, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 1, 'selected': False, 'text': "<p>Basically, once the TcpClient.Connect method has been successful, it will have created a mapping between the physical MAC address of the embedded system and the route it should take to that address (i.e. which network card to use).</p>\n\n<p>I don't believe that all messages then sent over the TcpClient connection will be sent out via both network cards.</p>\n\n<p>Do you have any data to suggest otherwise, or are you mealy guessing?</p>\n"}, {'answer_id': 49537, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': False, 'text': '<p>Xp maintains a routing table where it maps ranges of ip-adresses to networks and gateways.  </p>\n\n<p>you can view the table using "route print", with "route add" you can add a route to your embedded device.</p>\n'}, {'answer_id': 49545, 'author': 'Hath', 'author_id': 5186, 'author_profile': 'https://Stackoverflow.com/users/5186', 'pm_score': 3, 'selected': False, 'text': '<p>Try using a Socket for your client instead of the TcpClient Class.</p>\n\n<p>Then you can use Socket.Bind to target your local network adapter</p>\n\n<pre><code>    int port = 1234;\n\n    IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName());\n\n    //find ip address for your adapter here\n    IPAddress localAddress = entry.AddressList.FirstOrDefault();\n\n    IPEndPoint localEndPoint = new IPEndPoint(localAddress, port);\n\n    //use socket instead of a TcpClient\n    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n\n    //binds client to the local end point\n    client.Bind(localEndPoint);\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind.aspx</a></p>\n'}, {'answer_id': 60922, 'author': 'Murali Suriar', 'author_id': 6306, 'author_profile': 'https://Stackoverflow.com/users/6306', 'pm_score': 2, 'selected': False, 'text': "<p>If you have two network cards on the machine, then there shouldn't be a problem. Normal IP behaviour should ensure that traffic for your 'private' network (embedded systems in this case) is separate from your public network, without you having to do anything in your code. All that is required is for the two networks to be on different IP subnets, and for your 'public' NIC to be the default.</p>\n\n<p>Assuming your two NICs are configured as follows:</p>\n\n<pre><code>NIC A (Public): 192.168.1.10 mask 255.255.255.0\nNIC B (Private): 192.168.5.10 mask 255.255.255.0\n</code></pre>\n\n<p>The only configuration you need to verify is that NIC A is your default. When you try to send packets to any address in your private network (192.168.50.0 - 192.168.50.255), your IP stack will look in the routing table and see a directly connected network, and forward traffic via the private NIC. Any traffic to the (directly connected) public network will be sent to NIC A, as will traffic to any address for which you do not have a more specific route in your routing table.</p>\n\n<p>Your routing table (netstat -rn) should look something like this:</p>\n\n<pre><code>IPv4 Route Table\n===========================================================================\nActive Routes:\nNetwork Destination        Netmask          Gateway       Interface  Metric\n          0.0.0.0          0.0.0.0      192.168.1.1     192.168.1.10    266 &lt;&lt;--\n        127.0.0.0        255.0.0.0         On-link         127.0.0.1    306\n        127.0.0.1  255.255.255.255         On-link         127.0.0.1    306\n  127.255.255.255  255.255.255.255         On-link         127.0.0.1    306\n      169.254.0.0      255.255.0.0         On-link      192.168.1.10    286\n  169.254.255.255  255.255.255.255         On-link      192.168.1.10    266\n      192.168.1.0    255.255.255.0         On-link      192.168.1.10    266\n     192.168.1.10  255.255.255.255         On-link      192.168.1.10    266\n    192.168.1.255  255.255.255.255         On-link      192.168.1.10    266\n      192.168.5.0    255.255.255.0         On-link      192.168.5.10    266\n     192.168.5.10  255.255.255.255         On-link      192.168.5.10    266\n    192.168.5.255  255.255.255.255         On-link      192.168.5.10    266\n   255.255.255.255  255.255.255.255        On-link      192.168.1.10    276\n   255.255.255.255  255.255.255.255        On-link      192.168.5.10    276\n===========================================================================\n</code></pre>\n\n<p>There will also be some multicast routes (starting with 224) which have been omitted for brevity. The '&lt;&lt;--' indicates the default route, which should be using the public interface.</p>\n"}]
['https://Stackoverflow.com/questions/49507', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1898/']