expression
Async TCP EOF
A zero-length result with a nonempty buffer indicates EOF on the read half of this stream. Shutdown the sender write half after its payload, then read the known bytes and EOF. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): 120 0 Common mistake: Treating EOF as a temporary WouldBlock result.
Formal Syntax & Structure
rust syntaxrust
AsyncReadExt::read(&mut buf).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"x").await.unwrap(); client.shutdown().await.unwrap(); let mut b = [0; 1]; server.read_exact(&mut b).await.unwrap(); println!("{} {}", b[0], server.read(&mut b).await.unwrap()); }).await.expect("fixture deadline");}