socket2

Low-level, cross-platform utilities for creating and configuring network sockets in Rust.

Library
Cargo
v0.6.5
863stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
68/100Good
Development Activity60
Maintenance48
Community76
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
76/100Good
Architecture82
Code Quality84
Innovation78
Learning Curve60

socket2 is a Rust crate that provides direct access to advanced socket configuration options not available through the standard library’s networking types. It lets you create TCP, UDP, and other sockets and tune options such as timeouts, buffer sizes, keepalive, TTL, and address reuse across Unix and Windows platforms.

The crate aims to map as closely as possible onto the underlying system calls without adding error handling or abstraction, giving library and application authors the fine-grained control they need to build higher-level networking primitives while staying free of unsafe code.

What You Get

  • A Socket type covering socket creation across TCP, UDP, and other protocols and domains
  • Access to advanced per-socket options like keepalive, buffer sizes, TTL, timeouts, and address reuse
  • Cross-platform coverage of Unix and Windows behind a single API
  • SockAddr and SockRef helpers for address handling and borrowing existing sockets

Common Use Cases

  • Building async runtimes and networking libraries that need precise socket configuration
  • Setting socket options such as SO_REUSEADDR or keepalive that std::net does not expose
  • Writing cross-platform network servers and clients that require tuned socket behavior

Under The Hood

Architecture - The public API is defined in src/socket.rs (the Socket type), src/sockaddr.rs (SockAddr), and src/sockref.rs (SockRef), while platform-specific behavior is isolated behind a sys module (src/sys/unix.rs and a Windows counterpart). lib.rs re-exports the surface and selects the platform backend via cfg gates, so a single portable API delegates to per-OS system-call wrappers.

Tech Stack - It targets the Rust 2021 edition with a minimum Rust version of 1.70, using libc on Unix and the windows-sys bindings on Windows. It is a plain library crate with no build script and feature flags (such as all) that gate additional platform functionality.

Code Quality - The codebase is deliberately thin over the OS, mapping functions to system calls with minimal abstraction, and the project explicitly avoids unsafe in its public surface. An integration test suite lives under tests/ (tests/socket.rs) exercising real socket behavior, and the crate is maintained under the rust-lang organization with a CHANGELOG and contributing guide.

API Design - The Socket API is builder-like and predictable, with set_* and get_* methods that correspond directly to socket options developers already know from libc, plus conversions into std::net types. This makes it ergonomic for its intended audience of networking-library authors while intentionally staying wordy and explicit rather than opinionated.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search