Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::net::UdpSocket;
- use std::sync::mpsc::channel;
- use std::thread;
- use std::time::SystemTime;
- fn main() {
- // Create channels for sending and receieving
- let (one_tx, one_rx) = channel();
- let mut buf = [0; 1000000];
- // Spawn one second timer to sync the bytecounter to the screen and flush the values
- thread::spawn(move || {
- let mut now = SystemTime::now();
- let mut bytes_recieved = 0;
- loop {
- match now.elapsed() {
- Ok(elapsed) => {
- if elapsed.as_millis() > 1000 {
- println!("Cp/s: {}", &bytes_recieved);
- bytes_recieved = 0;
- now = SystemTime::now();
- } else {
- let _ = one_rx.try_recv().map(|reply| bytes_recieved += reply);
- }
- }
- Err(e) => {
- println!("Error: {:?}", e);
- }
- }
- }
- });
- loop {
- let socket = UdpSocket::bind("127.0.0.1:34254").expect("Failed to bind UDP port");
- // Receives a single datagram message on the socket. If `buf` is too small to hold
- // the message, it will be cut off.
- // nbr_bytes/source from the buffer:
- let (amt, _) = socket.recv_from(&mut buf).unwrap();
- one_tx.send(amt).unwrap();
- } // once the stream is closed, buffer is empty; we loop and try again
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement