Skip to main content

Module net

Module net 

Source
Expand description

Explicit nonblocking socket I/O backed by zio readiness waits.

Read/write/accept operations require a virtual caller. Numeric-address binding, socket options, and address inspection may also be used by OS callers. Hostname resolution is explicit and delegated; socket methods never resolve names. Readiness is advisory: every wake retries the actual nonblocking operation.

use vthread::{Runtime, net::{TcpListener, TcpStream}};
let listener = TcpListener::bind(([127, 0, 0, 1], 0).into())?;
let address = listener.local_addr()?;
let runtime = Runtime::new()?;
runtime.run_scope(|scope| {
    let mut server = scope.spawn("echo", move || {
        let (stream, _) = listener.accept()?;
        let mut data = [0; 4];
        stream.read_exact(&mut data)?;
        stream.write_all(&data)
    })?;
    let mut client = scope.spawn("client", move || {
        let stream = TcpStream::connect(address)?;
        stream.write_all(b"ping")?;
        let mut data = [0; 4];
        stream.read_exact(&mut data)?;
        assert_eq!(data, *b"ping");
        Ok::<_, vthread::Error>(())
    })?;
    client.join()??;
    server.join()??;
    Ok(())
})?;

Modules§

unix
Unix-domain streams and datagrams. Path connection is delegated to native workers; accept and data transfer use nonblocking readiness. Socket path cleanup is explicit.

Structs§

TcpListener
An owned nonblocking TCP listener; accept parks only its virtual caller.
TcpStream
An owned nonblocking TCP stream, shareable across virtual threads. Concurrent readers/writers follow OS stream semantics; they are not message framing.
UdpSocket
An owned UDP socket. Receive buffers may truncate a datagram as with std UDP.

Functions§

lookup_host
Resolves a hostname on the bounded native pool, rejecting more than limit results instead of silently truncating them. Resolver allocation is OS-owned. Cancellation cannot interrupt a resolver call already in progress.