mirror of
				https://github.com/tokio-rs/tokio.git
				synced 2025-11-03 14:02:47 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			32 lines
		
	
	
		
			842 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			32 lines
		
	
	
		
			842 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! 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:
 | 
						|
//!
 | 
						|
//!     ncat -l 6142
 | 
						|
//!
 | 
						|
//! And then in another terminal run:
 | 
						|
//!
 | 
						|
//!     cargo run --example hello_world
 | 
						|
 | 
						|
#![warn(rust_2018_idioms)]
 | 
						|
 | 
						|
use tokio::io::AsyncWriteExt;
 | 
						|
use tokio::net::TcpStream;
 | 
						|
 | 
						|
use std::error::Error;
 | 
						|
 | 
						|
#[tokio::main]
 | 
						|
pub async fn main() -> Result<(), Box<dyn Error>> {
 | 
						|
    // 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?;
 | 
						|
    println!("created stream");
 | 
						|
 | 
						|
    let result = stream.write_all(b"hello world\n").await;
 | 
						|
    println!("wrote to stream; success={:?}", result.is_ok());
 | 
						|
 | 
						|
    Ok(())
 | 
						|
}
 |