Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[macro_use]
- extern crate chan;
- use std::net::UdpSocket;
- fn main() {
- let tick = chan::tick_ms(1000);
- //let tock = chan::tick_ms(3000);
- let mut buf = [0; 10];
- let mut connections;
- loop {
- connections = 0;
- chan_select! {
- tick.recv() => {
- 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, src) = socket.recv_from(&mut buf).unwrap();
- println!("Recieved data from {}", &src);
- // Redeclare `buf` as slice of the received data and send reverse data back to origin.
- let buf = &mut buf[..amt];
- buf.reverse();
- let _ = socket.send_to(buf, &src);
- connections += 1;
- println!("LPS: {}", &connections);
- } //closed socket
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement