You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- title_markdown: "How do I access the client's TCP connection?"
3
+
body_markdown: |-
4
+
This is returned by the [`incoming()`](https://doc.rust-lang.org/std/net/struct.TcpListener.html#method.into_incoming) method.
5
+
6
+
```rust
7
+
# The return value is an iterator that provides a sequence of `TcpStream` objects.
8
+
for stream in listener.incoming()
9
+
```
10
+
11
+
Each [`TcpStream`](https://doc.rust-lang.org/std/net/struct.TcpStream.html) represents an active connection from a client.
12
+
13
+
- title_markdown: "How do I send a response to the client?"
14
+
body_markdown: |-
15
+
Use the [`write_all()`](https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all) method on the client's connection:
16
+
17
+
```rust
18
+
# Send the response to the client
19
+
stream.write_all(b"+PONG\r\n").unwrap();
20
+
```
21
+
22
+
- The above code uses `b` to create a [byte string literal](https://doc.rust-lang.org/reference/tokens.html#byte-string-literals).
23
+
- `+PONG\r\n` is the string "PONG" encoded as a [RESP simple string](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-strings).
24
+
- To satisfy the Rust compiler, you also need to import the [Write](https://doc.rust-lang.org/std/io/trait.Write.html) trait and mark the TcpStream as [mutable](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html) in order to use `write_all()`.
0 commit comments