Set nonblocking

This commit is contained in:
Ben Shiller 2025-06-29 13:26:38 -05:00
parent 2306735f42
commit b6fc0f312f
Signed by: shillerben
GPG Key ID: 7B4602B1FBF82986

View File

@ -1,8 +1,12 @@
use std::net::UdpSocket; use std::net::UdpSocket;
use std::io; use std::io;
use std::thread;
use std::time;
pub fn send(host: &str, message: &str, iters: u32) -> io::Result<()> { 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"); 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 packet_size = 1080;
let message = String::from(message); let message = String::from(message);
@ -11,8 +15,12 @@ pub fn send(host: &str, message: &str, iters: u32) -> io::Result<()> {
for _ in 0..iters { for _ in 0..iters {
match socket.send_to(buf.as_bytes(), host) { match socket.send(buf.as_bytes()) {
Ok(_) => (), 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), Err(e) => eprintln!("Failed to send message: {}", e),
} }
} }