Recommend the connect example over nc

This commit is contained in:
Alex Crichton
2017-09-11 08:07:38 -07:00
parent e0b751b013
commit 5e4cfdfab1
3 changed files with 9 additions and 10 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
//! //!
//! And then in another window run: //! And then in another window run:
//! //!
//! nc -4 localhost 8080 //! cargo run --example connect 127.0.0.1:8080
//! //!
//! You can run the second command in multiple windows and then chat between the //! You can run the second command in multiple windows and then chat between the
//! two, seeing the messages from the other client as they're received. For all //! two, seeing the messages from the other client as they're received. For all
+1 -1
View File
@@ -7,7 +7,7 @@
//! //!
//! and then in another terminal executing //! and then in another terminal executing
//! //!
//! nc -4 localhost 8080 //! cargo run --example connect 127.0.0.1:8080
//! //!
//! You should see `Hello!` printed out and then the `nc` program will exit. //! You should see `Hello!` printed out and then the `nc` program will exit.
+7 -8
View File
@@ -11,7 +11,7 @@
//! //!
//! And then you can connect to it via: //! And then you can connect to it via:
//! //!
//! nc -4 localhost 8080 > /dev/null //! cargo run --example connect 127.0.0.1:8080 > /dev/null
//! //!
//! You should see your CPUs light up as data's being shove into the ether. //! You should see your CPUs light up as data's being shove into the ether.
@@ -35,17 +35,16 @@ fn main() {
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
let addr = addr.parse::<SocketAddr>().unwrap(); let addr = addr.parse::<SocketAddr>().unwrap();
let mut l = Core::new().unwrap(); let mut core = Core::new().unwrap();
let socket = TcpListener::bind(&addr, &l.handle()).unwrap(); let handle = core.handle();
let socket = TcpListener::bind(&addr, &handle).unwrap();
println!("Listening on: {}", addr); println!("Listening on: {}", addr);
let server = socket.incoming().and_then(|(socket, addr)| { let server = socket.incoming().for_each(|(socket, addr)| {
println!("got a socket: {}", addr); println!("got a socket: {}", addr);
write(socket).or_else(|_| Ok(())) handle.spawn(write(socket).or_else(|_| Ok(())));
}).for_each(|()| {
println!("lost the socket");
Ok(()) Ok(())
}); });
l.run(server).unwrap(); core.run(server).unwrap();
} }
fn write(socket: TcpStream) -> IoFuture<()> { fn write(socket: TcpStream) -> IoFuture<()> {