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);
    a.join().unwrap();
    b.join().unwrap();
}

fn run_sender() {
    let socket = std::net::UdpSocket::bind("127.0.0.1:1234").unwrap();
    socket.connect("127.0.0.1:4321").unwrap();
    socket.send(&[0,1,2,3,4]).unwrap();
}

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

    dbg!(received);
    dbg!(socket_addr);
    dbg!(buf);
}

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\main.rs:17:62:
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\main.rs:5:14:
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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.