Skip to content

Instantly share code, notes, and snippets.

@cutecycle
Created August 7, 2020 09:11
Show Gist options
  • Save cutecycle/1daf66f652ac05aadec2bd26e4454bfe to your computer and use it in GitHub Desktop.
Save cutecycle/1daf66f652ac05aadec2bd26e4454bfe to your computer and use it in GitHub Desktop.
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