Skip to content

Commit 4aa9c69

Browse files
Demonstrate keepalive in the client (#12)
Demonstrate keepalive in the client
2 parents 62dec59 + c459613 commit 4aa9c69

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

examples/client.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ use async_std::{io, net, task};
33

44
fn main() -> Result<(), async_h1::Exception> {
55
task::block_on(async {
6-
let stream = net::TcpStream::connect("127.0.0.1:8080").await?;
7-
let (reader, writer) = &mut (&stream, &stream);
8-
let body = Body::from_string("hello chashu".to_owned());
9-
let mut req = client::encode(http::Request::new(body)).await?;
10-
io::copy(&mut req, writer).await?;
11-
let res = client::decode(reader).await?;
12-
println!("Response {:?}", res);
6+
let tcp_stream = net::TcpStream::connect("127.0.0.1:8080").await?;
7+
println!("connecting to {}", tcp_stream.peer_addr()?);
8+
9+
for i in 0usize..2 {
10+
let (tcp_reader, tcp_writer) = &mut (&tcp_stream, &tcp_stream);
11+
12+
println!("making request {}/2", i + 1);
13+
14+
let body = Body::from_string("hello chashu".to_owned());
15+
let mut req = client::encode(http::Request::new(body)).await?;
16+
io::copy(&mut req, tcp_writer).await?;
17+
18+
// read the response
19+
let res = client::decode(tcp_reader).await?;
20+
println!("{:?}", res);
21+
}
1322
Ok(())
1423
})
1524
}

examples/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ fn main() -> Result<(), async_h1::Exception> {
1212
while let Some(stream) = incoming.next().await {
1313
task::spawn(async {
1414
let stream = stream?;
15+
println!("starting new connection from {}", stream.peer_addr()?);
16+
1517
let (reader, writer) = &mut (&stream, &stream);
1618
server::connect(reader, writer, |_| {
1719
async {

0 commit comments

Comments
 (0)