Created
March 6, 2018 08:54
-
-
Save sgbasaraner/2400710a502a6f851eebd2eec359ea8d to your computer and use it in GitHub Desktop.
rust search broadcast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment