Module socket2
Low-level network socket programming beyond std::net.
socket2 provides direct access to system socket APIs without requiring unsafe code.
Where std::net offers high-level TCP/UDP types,
socket2 exposes the full range of socket options, address families,
and configuration that the OS provides.
Common uses include setting socket options like SO_REUSEADDR or TCP_NODELAY,
configuring keepalive, creating dual-stack IPv6 listeners,
and converting the configured socket into standard library types.
The crate is maintained by the Rust project itself.
Examples
Creating a TCP listener with socket options,
then converting to a std::net::TcpListener:
use ;
use SocketAddr;
// Create a TCP socket.
let socket = new?;
// Set SO_REUSEADDR so the port can be reused immediately after close.
socket.set_reuse_address?;
// Bind to a local address.
let address: SocketAddr = "127.0.0.1:0".parse.unwrap;
socket.bind?;
// Start listening.
socket.listen?;
// Convert to a std TcpListener for use with normal APIs.
let listener: TcpListener = socket.into;
let local_addr = listener.local_addr?;
assert_eq!;
# Ok::
Structs
- Domain Specification of the communication domain for a socket.
-
MaybeUninitSlice
A version of
IoSliceMutthat allows the buffer to be uninitialised. -
MsgHdr
Configuration of a
sendmsg(2)system call. -
MsgHdrMut
Configuration of a
recvmsg(2)system call. -
Protocol
Protocol specification used for creating sockets via
Socket::new. - RecvFlags Flags for incoming messages.
- TcpKeepalive Configures a socket's TCP keepalive parameters.
- Type Specification of communication semantics on a socket.