diff options
author | Teddy Wing | 2017-04-13 00:54:45 +0200 |
---|---|---|
committer | Teddy Wing | 2017-04-13 00:54:45 +0200 |
commit | a199d34222bd3abc8cda26449b4aa73c0994d656 (patch) | |
tree | ff71f2f501ee031c3db57a2e63e7e7df2d4e67d6 /src | |
parent | 77837552075762e94b9c8899d43e2f3857bd052e (diff) | |
download | HearURL-a199d34222bd3abc8cda26449b4aa73c0994d656.tar.bz2 |
main.rs: Try out TcpListener
Replicate the TcpListener example from the Rust standard library docs.
Using TCP instead of UDP allows us to read directly to a string and read
arbitrary length streams.
Here we just print the stream input to the console.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index f803e1d..b363a01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ use std::net::UdpSocket; use std::io; +use std::io::prelude::*; +use std::net::{TcpListener, TcpStream}; + fn make_socket() -> io::Result<()> { let mut socket = try!(UdpSocket::bind("127.0.0.1:34254")); @@ -16,6 +19,25 @@ fn make_socket() -> io::Result<()> { Ok(()) } +fn open_stream() -> io::Result<()> { + let listener = TcpListener::bind("127.0.0.1:34254")?; + + for stream in listener.incoming() { + match stream { + Ok(mut stream) => { + let mut url = String::new(); + stream.read_to_string(&mut url)?; + + println!("{}", url); + } + Err(e) => {} + } + } + + Ok(()) +} + fn main() { - make_socket(); + // make_socket(); + open_stream(); } |