C# 控制在哪个网卡上发送 TCP/IP 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:02:39  来源:igfitidea点击:

Controlling which Network Card TCP/IP message are sent on

提问by Skizz

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.

我目前正在开发的系统由一台运行 XP 的控制器 PC 和连接到一组嵌入式系统的 .Net 2 组成。所有这些组件都通过以太网网络相互通信。我目前在 XP 计算机上使用 TcpClient.Connect 打开与嵌入式系统的连接以发送 TCP/IP 消息。

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.

我现在必须将 XP 计算机连接到外部网络以发送处理数据,因此 XP 计算机上现在有两个网卡。但是,发送到外部网络的消息一定不能出现在将嵌入式系统连接在一起的网络上(不想消耗带宽),并且发送到嵌入式系统的消息一定不能出现在外部网络上。

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.

因此,我所做的断言是,当使用 TcpClient.Connect 方法时,发送到定义的 IP 地址的消息会在两个网卡上发送。

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.

如何指定通过哪些物理网卡消息发送,最好使用 .Net 网络 API。如果 .Net 中不存在这样的方法,那么我总是可以 P/Invoke Win32 API。

Skizz

斯基兹

回答by samjudson

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).

基本上,一旦 TcpClient.Connect 方法成功,它就会在嵌入式系统的物理 MAC 地址和它应该采取的到该地址的路由(即使用哪个网卡)之间创建映射。

I don't believe that all messages then sent over the TcpClient connection will be sent out via both network cards.

我不相信所有通过 TcpClient 连接发送的消息都会通过两个网卡发送出去。

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

您是否有任何数据可以提出其他建议,或者您只是在猜测?

回答by Mendelt

Xp maintains a routing table where it maps ranges of ip-adresses to networks and gateways.

Xp 维护一个路由表,它将 IP 地址范围映射到网络和网关。

you can view the table using "route print", with "route add" you can add a route to your embedded device.

您可以使用“路线打印”查看表格,使用“路线添加”您可以向嵌入式设备添加路线。

回答by Hath

Try using a Socket for your client instead of the TcpClient Class.

尝试为您的客户端使用 Socket 而不是 TcpClient 类。

Then you can use Socket.Bind to target your local network adapter

然后你可以使用 Socket.Bind 来定位你的本地网络适配器

    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

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

回答by Murali Suriar

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.

如果你的机器上有两个网卡,那么应该没有问题。正常的 IP 行为应确保您的“专用”网络(在这种情况下为嵌入式系统)的流量与您的公共网络分开,而您无需在代码中执行任何操作。所需要的只是两个网络位于不同的 IP 子网上,并且您的“公共”NIC 是默认的。

Assuming your two NICs are configured as follows:

假设您的两个 NIC 配置如下:

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 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.

您需要验证的唯一配置是 NIC A 是您的默认配置。当您尝试向专用网络中的任何地址 (192.168.50.0 - 192.168.50.255) 发送数据包时,您的 IP 堆栈将查看路由表并查看直接连接的网络,并通过专用 NIC 转发流量。到(直接连接的)公共网络的任何流量都将发送到 NIC A,到您的路由表中没有更具体路由的任何地址的流量也将发送到 NIC A。

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

您的路由表 (netstat -rn) 应如下所示:

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 some multicast routes (starting with 224) which have been omitted for brevity. The '<<--' indicates the default route, which should be using the public interface.

还将有一些多播路由(以 224 开头),为简洁起见已被省略。'<<--' 表示默认路由,应该使用公共接口。