Skip to content

Instantly share code, notes, and snippets.

@sgbasaraner
Created March 6, 2018 08:54
Show Gist options
  • Select an option

  • Save sgbasaraner/2400710a502a6f851eebd2eec359ea8d to your computer and use it in GitHub Desktop.

Select an option

Save sgbasaraner/2400710a502a6f851eebd2eec359ea8d to your computer and use it in GitHub Desktop.

Revisions

  1. sgbasaraner created this gist Mar 6, 2018.
    36 changes: 36 additions & 0 deletions yee.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    use std::str;
    use std::thread;
    use std::net::UdpSocket;

    fn main() {
    let addr = "239.255.255.250:1982";
    let socket = match UdpSocket::bind(addr) {
    Ok(s) => s,
    Err(e) => panic!("couldn't bind socket: {}", e)
    };

    let message =
    "M-SEARCH * HTTP/1.1\r\n
    HOST: 239.255.255.250:1982\r\n
    MAN: \"ssdp:discover\"\r\n
    ST: wifi_bulb".as_bytes();
    //socket.send_to(&message, addr).expect("couldn't send data");

    socket.send_to(message, addr).expect("couldn't send to socket");

    let mut buf = [0; 2048];
    loop {
    match socket.recv_from(&mut buf) {
    Ok((amt, src)) => {
    thread::spawn(move || {
    println!("amt: {}", amt);
    println!("src: {}", src);
    println!("{}", str::from_utf8(&buf).unwrap_or(""));
    });
    },
    Err(e) => {
    println!("couldn't receive a datagram: {}", e);
    }
    }
    }
    }