From e792fe13178fa5ec39294fc5bf53a3ecb5e43a1b Mon Sep 17 00:00:00 2001 From: bnewbold Date: Thu, 26 May 2016 16:51:32 -0400 Subject: init repo; basic uTP send/receive --- src/bin/iceftp.rs | 16 ++++++++++++++++ src/bin/iceftpd.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/bin/iceftp.rs create mode 100644 src/bin/iceftpd.rs (limited to 'src') diff --git a/src/bin/iceftp.rs b/src/bin/iceftp.rs new file mode 100644 index 0000000..3f19dbe --- /dev/null +++ b/src/bin/iceftp.rs @@ -0,0 +1,16 @@ + +extern crate utp; + +use utp::UtpSocket; + +fn main() { + // Bind to port 3540 + let addr = "127.0.0.1:3540"; + let mut socket = UtpSocket::connect(addr).expect("Error connecting to remote peer"); + + // Send a string + socket.send_to("Hi there!".as_bytes()).expect("Write failed"); + + // Close the socket + socket.close().expect("Error closing connection"); +} diff --git a/src/bin/iceftpd.rs b/src/bin/iceftpd.rs new file mode 100644 index 0000000..af29503 --- /dev/null +++ b/src/bin/iceftpd.rs @@ -0,0 +1,33 @@ + +extern crate utp; + +use utp::{UtpSocket, UtpListener}; +use std::str; + +fn main() { + // Connect to an hypothetical local server running on port 3540 + let addr = "127.0.0.1:3540"; + // Accept connection from anybody + let listener = UtpListener::bind(addr).expect("Error binding to local port"); + + for connection in listener.incoming() { + + let (mut socket, _src) = connection.unwrap(); + println!("Got connection from {}", socket.peer_addr().unwrap()); + + loop { + + let mut buf = [0; 1000]; + let (amt, _src) = socket.recv_from(&mut buf).ok().unwrap(); + if amt <= 0 { + break; + } + let buf = &buf[..amt]; + let s = str::from_utf8(buf).unwrap(); + println!("\tgot: {}", s); + + } + + } + +} -- cgit v1.2.3