Set nonblocking
This commit is contained in:
parent
2306735f42
commit
b6fc0f312f
10
src/send.rs
10
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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user