Incorrect behaviour of UdpSocket on windows?

std::net::UdpSocket::peek()/peek_from() doesn't seem to work properly on windows

fn main() {
    let b = std::thread::spawn(run_receiver);
    let a = std::thread::spawn(run_sender);

fn run_sender() {
    let socket = std::net::UdpSocket::bind("").unwrap();

fn run_receiver() {
    let socket = std::net::UdpSocket::bind("").unwrap();
    let mut buf = [0u8; 4];
    let (received, socket_addr) = socket.peek_from(&mut buf).unwrap();


This results in an 10040 error saying that the datagram might have been larger than the provided buffer. Isn't the purpose of peek() exactly to avoid this kind of situation?

Full error (OS error is not in english, it says what I said earlier)

thread '' panicked at src\
called Result::unwrap() on an Err value: Os { code: 10040, kind: Uncategorized, message: "Сообщение, отправленное на сокет датаграмм, было больше, чем буфер внутренних сообщений или был превышен иной сетевой параметр. Также возможно, что буфер для принятия сообщения был меньше, чем размер сообщения." }
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
thread 'main' panicked at src\
called Result::unwrap() on an Err value: Any { .. }

Maybe I'm doing something wrong?

UPD: Windows version is Windows 11 Home

Seems that's how UDP works on Windows.

1 Like