Last active
July 15, 2022 14:19
-
-
Save rsolomo/8703190 to your computer and use it in GitHub Desktop.
trying out creating a udp listener in Rust
This file contains 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::io::net::ip; | |
use std::io::net::udp; | |
use std::str; | |
fn main() { | |
let addr = ip::SocketAddr { | |
ip: ip::Ipv4Addr(127, 0, 0, 1), | |
port: 5514 | |
}; | |
let mut socket = match udp::UdpSocket::bind(addr) { | |
Ok(s) => s, | |
Err(e) => fail!("couldn't bind socket: {}", e) | |
}; | |
let mut buf = [0, ..2048]; | |
loop { | |
match socket.recv_from(buf) { | |
Ok((amt, src)) => spawn(proc() { | |
println!("amt: {}", amt); | |
println!("src: {}", src); | |
println!("{}", str::from_utf8(buf).unwrap_or("")); | |
}), | |
Err(e) => println!("couldn't recieve a datagram: {}", e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the code for the latest version of rust: