tokio::net module provides TCP, UDP, and Unix networking types for building asynchronous network applications.
Overview
This module contains networking types similar to those in the standard library, but designed for use with Tokio’s asynchronous runtime. All types are non-blocking and integrate with Tokio’s task system.Available Types
- TCP:
TcpListenerandTcpStreamfor TCP communication - UDP:
UdpSocketfor UDP communication - Unix Domain Sockets:
UnixListener,UnixStream, andUnixDatagram(Unix only) - Named Pipes: Available on Windows via
tokio::net::windows::named_pipe
TCP
TcpListener
A TCP socket server that listens for incoming connections.Methods
Creates a new TcpListener bound to the specified address.Parameters:
addr: impl ToSocketAddrs- The address to bind to
io::Result<TcpListener>Accepts a new incoming connection.Returns:
io::Result<(TcpStream, SocketAddr)>Returns the connected stream and the remote peer’s address. This method is cancel safe.Returns the local socket address this listener is bound to.Returns:
io::Result<SocketAddr>Creates a TcpListener from a
std::net::TcpListener.Parameters:listener: std::net::TcpListener- Must be in non-blocking mode
io::Result<TcpListener>TcpStream
A TCP stream between a local and remote socket.Methods
Opens a TCP connection to a remote host.Parameters:
addr: impl ToSocketAddrs- Address of the remote host
io::Result<TcpStream>Returns the local socket address.Returns:
io::Result<SocketAddr>Returns the remote socket address.Returns:
io::Result<SocketAddr>Waits for the socket to become readable.Returns:
io::Result<()>Usually paired with try_read(). This method is cancel safe.Tries to read data from the stream without blocking.Parameters:
buf: &mut [u8]- Buffer to read into
io::Result<usize> - Number of bytes readReturns WouldBlock error if the socket is not ready.Waits for the socket to become writable.Returns:
io::Result<()>Usually paired with try_write(). This method is cancel safe.Tries to write data to the stream without blocking.Parameters:
buf: &[u8]- Data to write
io::Result<usize> - Number of bytes writtenSets the value of the TCP_NODELAY option.Parameters:
nodelay: bool- If true, disables Nagle’s algorithm
io::Result<()>Gets the value of the TCP_NODELAY option.Returns:
io::Result<bool>Sets the value for the IP_TTL option.Parameters:
ttl: u32- Time-to-live value
io::Result<()>UDP
UdpSocket
A UDP socket for connectionless communication.Connection Modes
One-to-Many (bind): Usebind(), send_to(), and recv_from() to communicate with multiple addresses.
One-to-One (connect): Use connect() to associate with a single address, then use send() and recv().
Methods
Creates a UDP socket bound to the specified address.Parameters:
addr: impl ToSocketAddrs- Address to bind to
io::Result<UdpSocket>Connects the socket to a remote address.Parameters:
addr: impl ToSocketAddrs- Remote address
io::Result<()>After connecting, only send() and recv() can be used.Sends data to the specified address.Parameters:
buf: &[u8]- Data to sendtarget: impl ToSocketAddrs- Destination address
io::Result<usize> - Number of bytes sentReceives data and returns the sender’s address.Parameters:
buf: &mut [u8]- Buffer to receive into
io::Result<(usize, SocketAddr)>Returns the number of bytes read and the origin address.Sends data on a connected socket.Parameters:
buf: &[u8]- Data to send
io::Result<usize>The socket must be connected via connect() first.Receives data on a connected socket.Parameters:
buf: &mut [u8]- Buffer to receive into
io::Result<usize>Returns the local socket address.Returns:
io::Result<SocketAddr>Sharing UDP Sockets
UDP sockets can be shared across tasks usingArc without needing a Mutex:
Unix Domain Sockets
Unix domain sockets are only available on Unix platforms.
UnixListener
A Unix socket server listening for connections.UnixStream
A Unix stream socket.UnixDatagram
A Unix datagram socket.Error Handling
All networking operations returnio::Result<T> which can be handled using Rust’s ? operator:
See Also
- AsyncRead - Trait for asynchronous reading
- AsyncWrite - Trait for asynchronous writing
- ToSocketAddrs - Trait for address resolution