I'm trying to write a udp server that sends and receives udp packets. The server is supposed to work with DNS and multiple ip addresses when possible, so my idea was to simply send the packets to all ip addresses reported by DNS until I receive a packet from one of them to register it as the primary one.
Now here is where the problems occured:
My Laptop has 3 ip addresses, one for wifi, one for the ethernet port and one for a usb lan adapter.
For some reason when trying to send the udp packet to all 3 of these ips the server gets stuck in sendto for almost exactly 3 seconds after sending a few packets.
use std::{net::{UdpSocket, SocketAddr}, time::{Instant, Duration}};
use std::io::Write;
fn main() {
let sock = UdpSocket::bind("0.0.0.0:4242".parse::<SocketAddr>().unwrap()).unwrap();
let buf = [0u8; 10];
let mut last = Instant::now();
let mut i = 0;
loop {
sock.send_to(&buf, "192.168.178.125:4243".parse::<SocketAddr>().unwrap()).unwrap();
sock.send_to(&buf, "192.168.178.172:4243".parse::<SocketAddr>().unwrap()).unwrap();
sock.send_to(&buf, "192.168.178.189:4243".parse::<SocketAddr>().unwrap()).unwrap();
let current = Instant::now();
let dur = current.duration_since(last);
last = current;
if dur > Duration::from_millis(10) {
println!("\n{:?}", dur);
}
print!("{i}\r");
std::io::stdout().flush().unwrap();
i+=1;
}
}
--->
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/udp`
78
3.041038971s
157
3.039447585s
237
3.03970783s
316
3.039588757s
395
3.03958084s
475
3.039594867s
554
3.039486144s
Its also noteworthy, that the exact amount of bytes sent until this happens is consistent. It always hangs after exactly 78 packets sent.
Whats confusing me the most is the fact that this only happens when sending packets to more than one ip!
Even worse: It even blocks when the socket is set to NON_BLOCKING, which is the reason, I'm asking in the first place.
Whats going on here? Is there anyway I can prevent this?
I reproduced the issue in python as well so it is not a rust specific thing.
I also found previous questions stating, this may have something to do with arp requests.
However this makes me wounder, why it only happens when sending to multiple destinations.
I am not using a broadcast, because I wanted to specifically identify clients by ip address. (might consider it if nothing else works of course)