Despite binding two UDP sockets to 2 different addresses on different interfaces of a host, traffic flows through a single interface.
The topology of the network is as follows: 2 links between h2 and s1 1 link between h1 & s1
________
| |
h2 s1_______h1
|________|
I'm emulating 2 hosts and one switch on mininet. h1 is running a UDP server at 10.0.0.1:4243. The other host has 2 interfaces, with ips 10.0.0.2 & 10.0.0.4 . I'm making 2 sockets on h2, binding one to (10.0.0.2,9999) & the other socket to (10.0.0.4,8888). I'm running the code below, which should send packets on both interfaces alternately.
Instead, the first packets are sent on both the interfaces. All subsequent packets are sent over a single interface.
Client Code (Running on h2)
def client():
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock1.bind(("10.0.0.2",9999))
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock2.bind(("10.0.0.4",8888))
while True:
text = 'The time is {}'.format(datetime.now())
data = text.encode('ascii')
sock1.sendto(data, ('10.0.0.1', 4243))
data, address = sock1.recvfrom(MAX_BYTES)
sock2.sendto(data, ('10.0.0.1', 4243))
data, address = sock2.recvfrom(MAX_BYTES)
Server Code (Running on h1)
def server():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('10.0.0.1', 4243))
print('Listening at {}'.format(sock.getsockname()))
while True:
data, address = sock.recvfrom(MAX_BYTES)
text = data.decode('ascii')
print('The client at {} says {!r}'.format(address, text))
text = 'Your data was {} bytes long'.format(len(data))
data = text.encode('ascii')
sock.sendto(data, address)
One packet is sent over both interfaces of h2. Subsequent packets are all sent and received only via interface with IP 10.0.0.2. On wiresharking, half the packets have src/dest IPs set to 10.0.0.4