Skip to content

Commit a196f7c

Browse files
djcRalith
authored andcommitted
book: simplify connection setup constants
1 parent d948de6 commit a196f7c

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

docs/book/src/bin/set-up-connection.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
use quinn::{Endpoint, ServerConfig};
22
use std::error::Error;
3-
use std::net::SocketAddr;
3+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
44

5-
static SERVER_NAME: &str = "localhost";
6-
7-
fn client_addr() -> SocketAddr {
8-
"127.0.0.1:5000".parse::<SocketAddr>().unwrap()
9-
}
10-
11-
fn server_addr() -> SocketAddr {
12-
"127.0.0.1:5001".parse::<SocketAddr>().unwrap()
13-
}
5+
const SERVER_NAME: &str = "localhost";
6+
const LOCALHOST_V4: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
7+
const CLIENT_ADDR: SocketAddr = SocketAddr::new(LOCALHOST_V4, 5000);
8+
const SERVER_ADDR: SocketAddr = SocketAddr::new(LOCALHOST_V4, 5001);
149

1510
#[allow(dead_code, unused_variables)] // Included in `set-up-connection.md`
1611
async fn server(config: ServerConfig) -> Result<(), Box<dyn Error>> {
1712
// Bind this endpoint to a UDP socket on the given server address.
18-
let endpoint = Endpoint::server(config, server_addr())?;
13+
let endpoint = Endpoint::server(config, SERVER_ADDR)?;
1914

2015
// Start iterating over incoming connections.
2116
while let Some(conn) = endpoint.accept().await {
@@ -30,10 +25,10 @@ async fn server(config: ServerConfig) -> Result<(), Box<dyn Error>> {
3025
#[allow(dead_code, unused_variables)] // Included in `set-up-connection.md`
3126
async fn client() -> Result<(), Box<dyn Error>> {
3227
// Bind this endpoint to a UDP socket on the given client address.
33-
let endpoint = Endpoint::client(client_addr())?;
28+
let endpoint = Endpoint::client(CLIENT_ADDR)?;
3429

3530
// Connect to the server passing in the server name which is supposed to be in the server certificate.
36-
let connection = endpoint.connect(server_addr(), SERVER_NAME)?.await?;
31+
let connection = endpoint.connect(SERVER_ADDR, SERVER_NAME)?.await?;
3732

3833
// Start transferring, receiving data, see data transfer page.
3934

docs/book/src/quinn/set-up-connection.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It all starts with the [Endpoint][Endpoint] struct, this is the entry point of t
1212
Let's start by defining some constants.
1313

1414
```rust
15-
{{#include ../bin/set-up-connection.rs:5:13}}
15+
{{#include ../bin/set-up-connection.rs:5:8}}
1616
```
1717

1818
**Server**
@@ -22,7 +22,7 @@ The [server()][server] method, which can be used for this, returns the `Endpoint
2222
`Endpoint` is used to start outgoing connections and accept incoming connections.
2323

2424
```rust
25-
{{#include ../bin/set-up-connection.rs:16:28}}
25+
{{#include ../bin/set-up-connection.rs:11:23}}
2626
```
2727

2828
**Client**
@@ -32,7 +32,7 @@ The client needs to connect to the server using the [connect(server_name)][conne
3232
The `SERVER_NAME` argument is the DNS name, matching the certificate configured in the server.
3333

3434
```rust
35-
{{#include ../bin/set-up-connection.rs:31:41}}
35+
{{#include ../bin/set-up-connection.rs:26:36}}
3636
```
3737

3838
<br><hr>

0 commit comments

Comments
 (0)