leolong
leolong

Reputation: 192

Java Socket specify a certain network interface for outgoing connections

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

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.

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

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!

Upvotes: 7

Views: 4019

Answers (1)

user207421
user207421

Reputation: 310916

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

Upvotes: 5

Related Questions