expression
TCP half-close and response
Close the sending direction while retaining the ability to receive. A request can end with EOF while the peer still sends a response. Real protocols need explicit completion rules and deadlines. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): A Common mistake: Assuming shutdown of the write half immediately closes both directions.
Formal Syntax & Structure
rust syntaxrust
AsyncWriteExt::shutdown().awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { tokio::time::timeout(std::time::Duration::from_secs(3), async { use tokio::net::{TcpListener, TcpStream}; let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); let (client, accepted) = tokio::join!(TcpStream::connect(addr), listener.accept()); let mut client = client.unwrap(); let (mut server, _) = accepted.unwrap(); use tokio::io::{AsyncReadExt, AsyncWriteExt}; client.write_all(b"Q").await.unwrap(); client.shutdown().await.unwrap(); let mut req = [0; 1]; server.read_exact(&mut req).await.unwrap(); assert_eq!(server.read(&mut [0; 1]).await.unwrap(), 0); server.write_all(b"A").await.unwrap(); let mut ans = [0; 1]; client.read_exact(&mut ans).await.unwrap(); println!("{}", ans[0] as char); }).await.expect("fixture deadline");}