Java Socket为传出连接指定某个网络接口 [英] Java Socket specify a certain network interface for outgoing connections

查看:875
本文介绍了Java Socket为传出连接指定某个网络接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行。我之前也知道类似的问题,但这是不同的。

OK. I know similar questions were asked before, but this is different.

我注意到对类似问题的回答我可以使用 Socket.bind 为传出连接指定某个网络接口。 页面是关于该页面的官方说明。

I noticed from responses to similar questions that I can use Socket.bind to specify a certain network interface for outgoing connections. This page is an official instruction on that.

现在,我的机器有两个网卡 eth0 eth1 ,以及系统路由表将 eth0 设置为连接服务器的传出接口 S

Now, my machine has two NICs eth0 and eth1, and the system routing table sets eth0 as the outgoing interface for connecting to a server S.

然后我尝试了以下内容:

Then I tried the following:

Socket so = new Socket();
so.bind(new InetSocketAddress("ip.address.of.eth1", 0));
so.connect(new InetSocketAddress("ip.address.of.S", 80));

我使用WireShark捕获数据包,并注意到IP头的源地址字段是确实 ip.address.of.eth1 。但是通过检查以太网头,我注意到源MAC地址实际上是 eth0 的MAC地址,也就是说,数据包实际上仍然通过 eth0 !

I used WireShark to capture the packets, and noticed that the "Source Address" field of IP header was indeed ip.address.of.eth1. But by checking Ethernet headers, I noticed that the source MAC address is actually the MAC address of eth0, that is, the packets were actually still sent out via eth0!

任何人都可以帮忙解释为什么会出现这种行为?这是预期的吗?非常感谢!

Could anyone help explain why it had this behavior? Is it expected? Thanks a lot!

推荐答案

这是弱端系统模型的结果。这里描述的内容过于宽泛,但在 RFC 1122 中进行了讨论。

This is the result of the 'weak end system model'. It's too broad to describe here but it is discussed in RFC 1122.

这篇关于Java Socket为传出连接指定某个网络接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆