在同一盒子上的两个以太网接口之间发送数据

发布于 2024-08-22 09:37:33 字数 561 浏览 4 评论 0原文

我想在 Linux 中通过交叉电缆连接的两个以太网接口之间发送数据。两个以太网接口位于同一个盒子上。我将以太网接口定义为具有不同的静态 IP 地址(1.2.3.4 和 5.6.7.8),并使用套接字将字节从一个 IP 地址发送到另一个 IP 地址。我想强调的是,我希望数据离开盒子的一个接口,并在同一盒子的另一个接口上接收。这样做的后果之一是拔掉电缆会阻止同一盒子上的客户端和服务器之间的通信。

我猜内核比我聪明,它决定不需要通过线路发送信息并直接在内部套接字之间路由数据,从而否定测试。我尝试使用 SO_BINDTODEVICE 强制客户端从特定接口发送数据,但服务器从未看到它。我真的很困惑,这似乎不应该这么困难。

路由-n表中有两个条目

Dest       Gateway    Genmask        flags  metric  use  interface
1.2.3.0    0.0.0.0    255.255.255.0    U       0     0   eth0
5.6.7.0    0.0.0.0    255.255.255.0    U       0     0   eth1

I would like to send data between two ethernet interfaces that are connected with a crossover cable in Linux. The two ethernet interfaces are on the same box. I defined the ethernet interfaces to have different static ip addresses (1.2.3.4 and 5.6.7.8) and have been using sockets to send bytes from one IP address to the other. I want to emphasize that I want the data to leave the box one interface of the box and get received on the other interface of the same box. One consequence of this is that unplugging the cable would prevent communication between the client and server on the same box.

The kernel is smarter than me I guess and decides it doesn't need to send information out on the wire and routes the data directly between the sockets internally, thus negating the test. I have tried to use SO_BINDTODEVICE to force the client to send data out of a particular interface, but the server never sees it. I am really stuck and this doesn't seem like it should be this difficult.

There are two entries in the route -n table

Dest       Gateway    Genmask        flags  metric  use  interface
1.2.3.0    0.0.0.0    255.255.255.0    U       0     0   eth0
5.6.7.0    0.0.0.0    255.255.255.0    U       0     0   eth1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-08-29 09:37:33

如果不通过路由器,则无法使用 1.2.3.4/24 到 5.6.7.8/24 之间的 IP 进行通信。问题是IP只能与同一网段中的其他计算机通信。要计算网络地址,您需要在接口地址和子网掩码之间执行逻辑AND。这将为您提供网络地址。如果两个网络地址不同,则需要路由器。在您的示例中,您将拥有网络地址 1.2.3.0 和 5.6.7.0。因为这些不同,所以会发送数据。

更重要的是,大多数网络堆栈足够智能,可以看到如果两个接口位于同一台计算机上,则不会将数据一直发送到物理接口。它可能只会通过 IP 堆栈发送消息。但同样,它需要有效的地址才能工作。

您甚至应该能够仅使用环回网络设备来测试类似的设置。 (虚拟网卡。)

You can not communicate using IP between 1.2.3.4/24 to 5.6.7.8/24 without going though a router. The problem is that IP can only talk to other computers in the same network segment. To calulate the network address you need to do a logic AND between both the interface address and the subnet mask. This will give you the network addresses. If the two network addresses are different then a router will be required. In your example you will have network address 1.2.3.0 and 5.6.7.0. Because these are different it will will to send data.

More importantly most network stacks are smart enough to see that if both interfaces are on the same computer it will not send the data all the way to the phyical interface. It will probably only send the message though the IP stack. But again it would need to be vaild address for it to work.

You should even be able to test a similar setup using just loopback network devices. (virtual network cards.)

欢你一世 2024-08-29 09:37:33

尝试使用以下两个 IP 地址:

  • 192.168.64.1
  • 192.168.64.2

我不确定这是问题所在,但 1.2.3.4 和 5.6.7.8 不在同一子网上。 (因为当您将掩码应用于每个地址时,您不会获得相同的网络地址,如“目标”列所示。)

Try using the following two IP addresses:

  • 192.168.64.1
  • 192.168.64.2

I'm not sure this is the problem, but 1.2.3.4 and 5.6.7.8 aren't on the same subnet. (Because when you apply the mask to each one, you don't get the same network address, as the Dest column is showing you.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文