aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2017-03-16 04:34:42 +0100
committerTeddy Wing2017-03-16 04:34:42 +0100
commit77837552075762e94b9c8899d43e2f3857bd052e (patch)
tree27c25bf0d0d314995967ce5efc8fcaade66edc97 /src/main.rs
parenta221568153b3e047be330ce29d54c0a6a19c3bb5 (diff)
downloadHearURL-77837552075762e94b9c8899d43e2f3857bd052e.tar.bz2
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.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs20
1 files changed, 19 insertions, 1 deletions
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();
}