Advertisement
Dijit

Untitled

Mar 3rd, 2020
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.17 KB | None | 0 0
  1. #[macro_use]
  2. extern crate chan;
  3.  
  4. use std::net::UdpSocket;
  5.  
  6.  
  7. fn main() {
  8.     let tick = chan::tick_ms(1000);
  9.     //let tock = chan::tick_ms(3000);
  10.     let mut buf = [0; 10];
  11.     let mut connections;
  12.  
  13.     loop {
  14.         connections = 0;
  15.         chan_select! {
  16.             tick.recv() => {
  17.                 loop {
  18.                     let socket = UdpSocket::bind("127.0.0.1:34254").expect("Failed to bind UDP port");
  19.                     // Receives a single datagram message on the socket. If `buf` is too small to hold
  20.                     // the message, it will be cut off.
  21.                     // nbr_bytes/source from the buffer:
  22.                     let (amt, src) = socket.recv_from(&mut buf).unwrap();
  23.                     println!("Recieved data from {}", &src);
  24.  
  25.                     // Redeclare `buf` as slice of the received data and send reverse data back to origin.
  26.                     let buf = &mut buf[..amt];
  27.                     buf.reverse();
  28.                     let _ = socket.send_to(buf, &src);
  29.                     connections += 1;
  30.                     println!("LPS: {}", &connections);
  31.                 } //closed socket
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement