mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-23 03:53:05 +00:00
Make language more concise / readable
This commit is contained in:
parent
25b53f7991
commit
eaf2abc6bf
1 changed files with 9 additions and 23 deletions
32
src/net.md
32
src/net.md
|
@ -544,18 +544,8 @@ quick_main!(run);
|
||||||
|
|
||||||
[![std-badge]][std] [![cat-net-badge]][cat-net]
|
[![std-badge]][std] [![cat-net-badge]][cat-net]
|
||||||
|
|
||||||
Give the OS the responsibility to pick an unused [registered port]. These ports
|
In this example, the port is displayed on the console, and the program will
|
||||||
generally do not require sudo permission to bind to, and can be helpful when
|
listen until a request is made.
|
||||||
establishing connections with clients in a disposable communication chain.
|
|
||||||
These ports begin with 1024 and have around 48,000 available. Typically, a
|
|
||||||
known port is used to initiate the communication and subsequent communication
|
|
||||||
occurs on the registered port after a handshake process.
|
|
||||||
|
|
||||||
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
|
|
||||||
up.
|
|
||||||
|
|
||||||
```rust, no_run
|
```rust, no_run
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -590,19 +580,15 @@ quick_main!(run);
|
||||||
```
|
```
|
||||||
|
|
||||||
The `std` library is leveraged to make a well formed IP/port with the
|
The `std` library is leveraged to make a well formed IP/port with the
|
||||||
[`SocketAddrV4`] and [`Ipv4Addr`] structs. The loopback address is a special
|
[`SocketAddrV4`] and [`Ipv4Addr`] structs. An unused random port is requested
|
||||||
address that runs only on the local machine, and is available on all machines.
|
by passing 0 to [`TcpListener::bind`]. The assigned address is available via
|
||||||
|
|
||||||
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
|
|
||||||
[`TcpListener::local_addr`].
|
[`TcpListener::local_addr`].
|
||||||
|
|
||||||
The rest of the recipe prints out the request made on the socket.
|
[`TcpListener::accept`] synchronously waits for an incoming connection and
|
||||||
[`TcpListener::accept`] returns a `(`[`TcpStream`], [`SocketAddrV4`]`)`
|
returns a `(`[`TcpStream`], [`SocketAddrV4`]`)` representing the request.
|
||||||
representing the data from the client, its IP address and port. Reading on the
|
Reading on the socket with [`read_to_string`] will wait until the connection is
|
||||||
socket with [`read_to_string`] will wait until the connection is closed which
|
closed which can be tested with `telnet ip port`. For example, if the program
|
||||||
can be tested with `telnet ip port`. For example, if the program shows
|
shows Listening on 127.0.0.1:11500, run
|
||||||
Listening on 127.0.0.1:11500, run
|
|
||||||
|
|
||||||
`telnet 127.0.0.1 11500`
|
`telnet 127.0.0.1 11500`
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue