Files
tokio/examples/hello_world.rs
T

32 lines
842 B
Rust
Raw Normal View History

2019-07-09 20:21:12 +02:00
//! A simple client that opens a TCP stream, writes "hello world\n", and closes
//! the connection.
//!
//! To start a server that this client can talk to on port 6142, you can use this command:
2019-07-09 20:21:12 +02:00
//!
//! ncat -l 6142
//!
//! And then in another terminal run:
//!
//! cargo run --example hello_world
#![warn(rust_2018_idioms)]
2019-07-09 20:21:12 +02:00
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
2019-07-25 16:47:31 -04:00
use std::error::Error;
2019-07-09 20:21:12 +02:00
#[tokio::main]
2019-07-25 16:47:31 -04:00
pub async fn main() -> Result<(), Box<dyn Error>> {
2019-07-09 20:21:12 +02:00
// Open a TCP stream to the socket address.
//
// Note that this is the Tokio TcpStream, which is fully async.
let mut stream = TcpStream::connect("127.0.0.1:6142").await?;
2019-07-09 20:21:12 +02:00
println!("created stream");
2019-07-10 14:21:20 -07:00
let result = stream.write_all(b"hello world\n").await;
2019-07-09 20:21:12 +02:00
println!("wrote to stream; success={:?}", result.is_ok());
2019-07-25 16:47:31 -04:00
Ok(())
2019-07-09 20:21:12 +02:00
}