Control which TCP / IP message for a network card is sent

I am currently working on a system running XP, with .Net 2 connected to a set of embedded systems. All of these components communicate with each other over an Ethernet network. I am currently using TcpClient.Connect on an XP computer to open a connection to embedded systems to send TCP / IP messages.

Now I need to connect the XP computer to an external network to send processing data, so now there are two network cards on the XP computer. However, messages sent to the external network should not appear on the network connecting the embedded systems together (do not want to consume bandwidth), and messages to the embedded systems should not appear on the external network.

So, the statement I am making is that messages sent to a specific IP address are sent on both network cards using the TcpClient.Connect method.

How to specify which physical network card messages are sent through, ideally using the network API.Net. If such a method does not exist in .Net, I can always P / call the Win32 API.

Skizz

+4
source share
4 answers

Try using Socket for your client instead of the TcpClient class.

Then you can use Socket.Bind to configure the local network adapter

int port = 1234; IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName()); //find ip address for your adapter here IPAddress localAddress = entry.AddressList.FirstOrDefault(); IPEndPoint localEndPoint = new IPEndPoint(localAddress, port); //use socket instead of a TcpClient Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //binds client to the local end point client.Bind(localEndPoint); 

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind.aspx

+5
source

If you have two network cards on your computer, then there should be no problem. The usual IP behavior is to ensure that the traffic for your 'private' network (embedded systems in this case) is separated from your public network, without having to do anything in the code. All that is required is for the two networks to be on different IP subnets, and by default for your โ€œpublicโ€ network adapter.

Assuming your two network adapters are configured as follows:

 NIC A (Public): 192.168.1.10 mask 255.255.255.0 NIC B (Private): 192.168.5.10 mask 255.255.255.0 

The only configuration you should check is that NIC A is your default value. When you try to send packets to any address on 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 through a private network adapter. Any traffic to the (directly connected) public network will be sent to NIC A, and there will also be traffic to any address for which you do not have a more specific route in your routing table.

The routing table (netstat -rn) should look something like this:

 IPv4 Route Table =========================================================================== Active Routes: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.10 266 <<-- 127.0.0.0 255.0.0.0 On-link 127.0.0.1 306 127.0.0.1 255.255.255.255 On-link 127.0.0.1 306 127.255.255.255 255.255.255.255 On-link 127.0.0.1 306 169.254.0.0 255.255.0.0 On-link 192.168.1.10 286 169.254.255.255 255.255.255.255 On-link 192.168.1.10 266 192.168.1.0 255.255.255.0 On-link 192.168.1.10 266 192.168.1.10 255.255.255.255 On-link 192.168.1.10 266 192.168.1.255 255.255.255.255 On-link 192.168.1.10 266 192.168.5.0 255.255.255.0 On-link 192.168.5.10 266 192.168.5.10 255.255.255.255 On-link 192.168.5.10 266 192.168.5.255 255.255.255.255 On-link 192.168.5.10 266 255.255.255.255 255.255.255.255 On-link 192.168.1.10 276 255.255.255.255 255.255.255.255 On-link 192.168.5.10 276 =========================================================================== 

There will also be several multicast routes (starting at 224) that were omitted for brevity. "<<-" indicates the default route that the public interface should use.

+2
source

Basically, once the TcpClient.Connect method is successful, it will create a mapping between the physical MAC address of the embedded system and the route that it should follow at that address (for example, which network card to use).

I do not believe that all messages sent over the TcpClient connection will be sent through both network cards.

Do you have any data to suggest otherwise, or are you painfully guessing?

+1
source

Xp maintains a routing table in which it displays the ranges of IP addresses on the network and gateways.

You can view the table using "print route", with "add route" you can add a route to the built-in device.

+1
source

Source: https://habr.com/ru/post/1276593/


All Articles