mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 19:43:06 +00:00
fixed spaces, added telnet example, and properly returned errors
This commit is contained in:
parent
0436206ec1
commit
e5c7db1d10
1 changed files with 12 additions and 14 deletions
14
src/net.md
14
src/net.md
|
@ -577,16 +577,12 @@ fn run() -> Result<()>{
|
|||
let listen = TcpListener::bind(socket)?;
|
||||
let port = listen.local_addr()?;
|
||||
println!("Listening on {}, access this port to end the program", port);
|
||||
match listen.accept() {
|
||||
Ok((mut tcp_stream, addr)) => {
|
||||
// read from the socket until connection closed by client.
|
||||
let (mut tcp_stream, addr) = listen.accept()?; //block until requested
|
||||
let mut input = String::new();
|
||||
let _ = tcp_stream.read_to_string(&mut input); //discard byte count
|
||||
// read from the socket until connection closed by client.
|
||||
let _ = tcp_stream.read_to_string(&mut input)?; //discard byte count
|
||||
println!("Connection received! \r\n{:?} says {}", addr, input);
|
||||
Ok(())
|
||||
},
|
||||
Err(e) => { Err(e.into()) }
|
||||
}
|
||||
}
|
||||
|
||||
quick_main!(run);
|
||||
|
@ -604,7 +600,9 @@ The rest of the recipe prints out the request made on the socket.
|
|||
`TcpListener::accept` returns a tuple of `TcpStream` and client information
|
||||
within a `Result`. The `Read` implmentation is used on that `TcpStream` to
|
||||
collect the request payload. Closing the program is as easy as browsing to the
|
||||
ip:port or using telnet to send some data, pressing ctrl-] and typing quit.
|
||||
ip:port or using `telnet ip port` to send some data. For example, if the
|
||||
program shows Listening on 127.0.0.1:11500, run `telnet 127.0.0.1 11500`. After
|
||||
sending data in telnet press `ctrl-]` and type `quit`.
|
||||
<!-- Categories -->
|
||||
|
||||
[cat-encoding-badge]: https://img.shields.io/badge/-encoding-red.svg
|
||||
|
|
Loading…
Reference in a new issue