-
Notifications
You must be signed in to change notification settings - Fork 303
Added networking example to listen on random socket using tcp/ip #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added networking example to listen on random socket using tcp/ip #137
Conversation
src/net.md
Outdated
let listen = TcpListener::bind(socket)?; | ||
let port = listen.local_addr()?; | ||
println!("Listening on {}, access this port to end the program", port); | ||
match listen.accept() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use listen.accept()?
to handle this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it idiomatic to keep the match? I've refactored it to this:
match listen.accept()? {
(mut tcp_stream, addr) => {
// read from the socket until connection closed by client.
let mut input = String::new();
let _ = tcp_stream.read_to_string(&mut input)?; //discard byte count
println!("Connection received! \r\n{:?} says {}", addr, input);
Ok(())
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this work?
let (mut tcp_stream, addr) = listen.accept()?;
src/net.md
Outdated
Ok((mut tcp_stream, addr)) => { | ||
// read from the socket until connection closed by client. | ||
let mut input = String::new(); | ||
let _ = tcp_stream.read_to_string(&mut input); //discard byte count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can return an error, please handle it with ?
.
src/net.md
Outdated
`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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please give the telnet command to run here so that users can tests this.
src/net.md
Outdated
} | ||
} | ||
|
||
fn run() -> Result<()>{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be one space before Result<()>
and one space after.
src/net.md
Outdated
#[macro_use] | ||
extern crate error_chain; | ||
|
||
use std::net::{SocketAddrV4, Ipv4Addr,TcpListener}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put one space after both commas.
src/net.md
Outdated
} | ||
|
||
fn run() -> Result<()>{ | ||
let loopback = Ipv4Addr::new(127,0,0,1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust style is to put spaces after commas.
Hey @budziq could you take over the review for this one? LMK when it's ready to merge. Thanks so much! |
Will do. I'll be near a pc later today |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndyGauge The example is very nice! I would suggest only minor changes mostly to make the description more concise.
src/net.md
Outdated
let loopback = Ipv4Addr::new(127, 0, 0, 1); | ||
// Assigning port 0 requests the OS to assign a free port | ||
let socket = SocketAddrV4::new(loopback, 0); | ||
let listen = TcpListener::bind(socket)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest renaming listen to listener
src/net.md
Outdated
let port = listen.local_addr()?; | ||
println!("Listening on {}, access this port to end the program", port); | ||
let (mut tcp_stream, addr) = listen.accept()?; //block until requested | ||
let mut input = String::new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the connection is already accepted here. I would suggest printing
println!("Connection received on {:?}? says {}", addr);
here as it shows exactly where synchronous waiting occurs.
src/net.md
Outdated
let mut input = String::new(); | ||
// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would drop the \r\n and just use
println!("{:?} says {}", addr, input);
here
src/net.md
Outdated
let (mut tcp_stream, addr) = listen.accept()?; //block until requested | ||
let mut input = String::new(); | ||
// read from the socket until connection closed by client. | ||
let _ = tcp_stream.read_to_string(&mut input)?; //discard byte count |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rephrase the comment above to include the
//discard byte count
src/net.md
Outdated
``` | ||
|
||
The `std` library is leveraged to make a well formed IP/port with the | ||
`SocketAddrV4` and `Ipv4Addr` structs. The loopback address is a special |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add hyperlinks for SocketAddrV4
and Ipv4Addr
src/net.md
Outdated
[`TcpListener::local_addr`]. | ||
|
||
The rest of the recipe prints out the request made on the socket. | ||
`TcpListener::accept` returns a tuple of `TcpStream` and client information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add hyperlinks for TcpListener::accept
and TcpStream
also what "client information"? I would suggest just writing:
(TcpStream, SocketAddr)
with appropriate hyperlinks
src/net.md
Outdated
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 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest giving the telnet 127.0.0.1 11500
a separate line
src/net.md
Outdated
|
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest dropping the information about Result
once hyperlinks are added to other structs and functions as it makes the description overly wordy
src/net.md
Outdated
[`TcpListener::local_addr`]. | ||
|
||
The rest of the recipe prints out the request made on the socket. | ||
`TcpListener::accept` returns a tuple of `TcpStream` and client information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest using passive voice and not describing what function returns what as reader will be able to just follow your hyperlinks. But rather write what is happening in a concise 2-4 sentence description.
src/net.md
Outdated
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest rewording the description to. "Reading on the socket with read_to_string
will wait untill connection is closed" which can be tested with telnet ip port
command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there. Sorry for waiting with the rest of the review but I did not want to overwhelm you. This is almost perfect already and fixing these will make it merge ready so do not give up!
src/net.md
Outdated
|
||
[![std-badge]][std] [![cat-net-badge]][cat-net] | ||
|
||
Give the OS the responsibility to pick an unused [registered port]. These ports |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that you have put a lot of effort into this section but I would rather not educate readers on CS/Networking basics. We can safely assume that reader understands these concepts and came here only for the rust snippet and its very brief textual description (that would ideally be 2-4 sentences). I would suggest dropping this paragraph completely.
src/net.md
Outdated
|
||
The `std` library is leveraged to make a well formed IP/port with the | ||
[`SocketAddrV4`] and [`Ipv4Addr`] structs. The loopback address is a special | ||
address that runs only on the local machine, and is available on all machines. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather not try to educate the readers on what the loopback interface is. I would just stick to stating that code is listening on the loopback interface (possibly with hyperlink to some authoritative resource)
src/net.md
Outdated
In this example, the port is displayed on the console, and will listen until a | ||
request is made. If you use your browser to test this program, simply enter | ||
the address:port into your location bar and make the request. Because the | ||
program returns nothing, the browser's stop feature can be used to speed things |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This paragraph is overly word and redundant with later telnet testing one.
I would remove all of it except the first sentence.
src/net.md
Outdated
port. The address and port that the OS assigns is available using | ||
[`TcpListener::local_addr`]. | ||
|
||
The rest of the recipe prints out the request made on the socket. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this sentence and modify the following ones to reflect what is going one step by step.
src/net.md
Outdated
[`SocketAddrV4`] and [`Ipv4Addr`] structs. The loopback address is a special | ||
address that runs only on the local machine, and is available on all machines. | ||
|
||
By passing 0 to the [`TcpListener::bind`], the OS will assign an unused random |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest rewording it in a more concise manner e.g:
An unused random port is requested by passing 0 to the [
TcpListener::bind
]. The assigned address is available via [TcpListener::local_addr
].
src/net.md
Outdated
[`TcpListener::local_addr`]. | ||
|
||
The rest of the recipe prints out the request made on the socket. | ||
[`TcpListener::accept`] returns a `(`[`TcpStream`], [`SocketAddrV4`]`)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key fact about accept is that it synchronously waits for an incoming connection and returns the pair
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'an incoming connection is waited synchronously by TcpListener::accept` <- another case where passive isn't the best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
src/net.md
Outdated
|
||
The rest of the recipe prints out the request made on the socket. | ||
[`TcpListener::accept`] returns a `(`[`TcpStream`], [`SocketAddrV4`]`)` | ||
representing the data from the client, its IP address and port. Reading on the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data from the client, its IP address and port.
Could be misunderstood. I would suggest something like:
"representing client connection IP address and port."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so I'm going to take out the details about what the tuple is and call it the request. I think the links do a good job of explaining the details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work:
[TcpListener::accept
] synchronously waits for an incoming connection and
returns a (
[TcpStream
], [SocketAddrV4
])
representing the request.
[read_to_string
] will wait until the connection is closed, flushing the socket,
which can be tested with telnet ip port
. For example, if the program
shows Listening on 127.0.0.1:11500, run
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks excellent 👍
src/net.md
Outdated
address that runs only on the local machine, and is available on all machines. | ||
|
||
By passing 0 to the [`TcpListener::bind`], the OS will assign an unused random | ||
port. The address and port that the OS assigns is available using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit. Please do not mix tenses. Use present/past/future throughout the whole text.
Ok drop the passive voice comments ;) https://github.com/brson/rust-cookbook/issues/139 |
Do you want me to rebase? |
Nice work! @AndyGauge LGTM @dtolnay |
Hold on, registered port reference exists. |
whew. Thanks @budziq |
No problem. Keep them coming @AndyGauge ! |
Great work. |
Fixes #123