From 77837552075762e94b9c8899d43e2f3857bd052e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 16 Mar 2017 04:34:42 +0100 Subject: main.rs: Copy UdpSocket example to test out sockets Copies the example given at https://doc.rust-lang.org/std/net/struct.UdpSocket.html to experiment with receiving and sending data over the socket. --- src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e7a11a9..f803e1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,21 @@ +use std::net::UdpSocket; +use std::io; + +fn make_socket() -> io::Result<()> { + let mut socket = try!(UdpSocket::bind("127.0.0.1:34254")); + + // read from the socket + let mut buf = [0; 10]; + let (amt, src) = try!(socket.recv_from(&mut buf)); + + // send a reply to the socket we received data from + let buf = &mut buf[..amt]; + buf.reverse(); + try!(socket.send_to(buf, &src)); + + Ok(()) +} + fn main() { - println!("Hello, world!"); + make_socket(); } -- cgit v1.2.3