[SOLVED] Help me listen to broadcast (Windows)


#1

I am not able to bind to broadcast because I don’t know how to set the broadcast option before binding.

Here is my code:

use std::net::UdpSocket;

fn listen_to_broadcasts() -> std::io::Result<()> {
    // How do I swap the following two lines ???
    let mut socket = try!(UdpSocket::bind("255.255.255.255:8892"));
    try!(socket.set_broadcast(true));

    let mut buf = [0; 10];
    let (bytes_count, source) = try!(socket.recv_from(&mut buf));
    println!("Recieved {} from {:?}", bytes_count, source);
    Ok(())
}

fn main() {
    println!("Hello!");
    println!("{:?}", listen_to_broadcasts());
    println!("Bye!");
}

The results:
Err(Error { repr: Os { code: 10049, message: "The requested address is not valid in its context." } })


#2

Did you try to bind on ‘0.0.0.0:8892’? You may have to disable the Windows firewall.

EDIT: I just tested and it works with 0.0.0.0 (even without socket.set_broadcast(true)).
EDIT2: It’s weird that the Windows firewall didn’t ask me if I wanted to allow this process. Maybe it can be added manually.


#3

0.0.0.0 works. For some reason I thought I should listen to broadcast on the broadcast address :sweat_smile:

Thanks :thumbsup: