Created
August 7, 2020 09:11
-
-
Save cutecycle/1daf66f652ac05aadec2bd26e4454bfe to your computer and use it in GitHub Desktop.
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::net::UdpSocket; | |
fn main() -> std::io::Result<()> { | |
{ | |
let mut socket = UdpSocket::bind("127.0.0.1:34254")?; | |
// Receives a single datagram message on the socket. If `buf` is too small to hold | |
// the message, it will be cut off. | |
let mut buf = [0; 10]; | |
let (amt, src) = socket.recv_from(&mut buf)?; | |
// Redeclare `buf` as slice of the received data and send reverse data back to origin. | |
let buf = &mut buf[..amt]; | |
buf.reverse(); | |
socket.send_to(buf, &src)?; | |
} // the socket is closed here | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment