forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tokio: rewrite examples with
async
. (tokio-rs#1228)
- Loading branch information
1 parent
f529928
commit 8279518
Showing
5 changed files
with
81 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Hello world server. | ||
//! | ||
//! A simple client that opens a TCP stream, writes "hello world\n", and closes | ||
//! the connection. | ||
//! | ||
//! You can test this out by running: | ||
//! | ||
//! ncat -l 6142 | ||
//! | ||
//! And then in another terminal run: | ||
//! | ||
//! cargo run --example hello_world | ||
#![deny(warnings, rust_2018_idioms)] | ||
#![feature(async_await)] | ||
|
||
use tokio; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::net::TcpStream; | ||
|
||
#[tokio::main] | ||
pub async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let addr = "127.0.0.1:6142".parse()?; | ||
|
||
// Open a TCP stream to the socket address. | ||
// | ||
// Note that this is the Tokio TcpStream, which is fully async. | ||
let mut stream = TcpStream::connect(&addr).await?; | ||
println!("created stream"); | ||
let result = stream.write(b"hello world\n").await; | ||
println!("wrote to stream; success={:?}", result.is_ok()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.