diff --git a/src/send.rs b/src/send.rs index 2ab9839..9d84f38 100644 --- a/src/send.rs +++ b/src/send.rs @@ -1,8 +1,12 @@ use std::net::UdpSocket; use std::io; +use std::thread; +use std::time; pub fn send(host: &str, message: &str, iters: u32) -> io::Result<()> { let socket = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind socket"); + socket.connect(host).expect("Connect failed"); + socket.set_nonblocking(true).expect("Failed to set socket as non-blocking"); let packet_size = 1080; let message = String::from(message); @@ -11,8 +15,12 @@ pub fn send(host: &str, message: &str, iters: u32) -> io::Result<()> { for _ in 0..iters { - match socket.send_to(buf.as_bytes(), host) { + match socket.send(buf.as_bytes()) { Ok(_) => (), + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + println!("send_to would have blocked"); + thread::sleep(time::Duration::from_nanos(1)); + }, Err(e) => eprintln!("Failed to send message: {}", e), } }