1 | //! A simple client that opens a TCP stream, writes "hello world\n", and closes |
2 | //! the connection. |
3 | //! |
4 | //! To start a server that this client can talk to on port 6142, you can use this command: |
5 | //! |
6 | //! ncat -l 6142 |
7 | //! |
8 | //! And then in another terminal run: |
9 | //! |
10 | //! cargo run --example hello_world |
11 | |
12 | #![warn (rust_2018_idioms)] |
13 | |
14 | use tokio::io::AsyncWriteExt; |
15 | use tokio::net::TcpStream; |
16 | |
17 | use std::error::Error; |
18 | |
19 | #[tokio::main] |
20 | pub async fn main() -> Result<(), Box<dyn Error>> { |
21 | // Open a TCP stream to the socket address. |
22 | // |
23 | // Note that this is the Tokio TcpStream, which is fully async. |
24 | let mut stream = TcpStream::connect("127.0.0.1:6142" ).await?; |
25 | println!("created stream" ); |
26 | |
27 | let result = stream.write_all(b"hello world \n" ).await; |
28 | println!("wrote to stream; success={:?}" , result.is_ok()); |
29 | |
30 | Ok(()) |
31 | } |
32 | |