A pure C# udp socket library with zero external dependencies.
- NativeSockets is a lightweight, cross‑platform library for udp networking.
- It is written entirely in managed C# and does not rely on any third‑party binaries or platform‑specific packages.
- The library provides a consistent API across all supported operating systems, with automatic runtime adaptation to the underlying environment.
- Windows
- Linux
- Android
- macOS
- iOS
- tvOS
- watchOS
- visionOS
- FreeBSD
- Zero garbage collection pressure
- Pure managed code – No external dependencies to deploy or manage.
- Complete udp support – create, bind, connect, send, receive, and poll sockets.
- Ipv4 and Ipv6 with dual‑mode support.
- Scatter/gather I/O – Efficient vectored send and receive operations.
- The library works out‑of‑the‑box on any supported platform.
- There are no additional runtime libraries, native binaries, or platform‑specific packages to install – just add the NuGet package and start using it.
A P/Invoke wrapper over a native C UDP socket library, providing the same API as NativeSockets with a native backend.
- NativeSockets2 is a lightweight, cross‑platform library for UDP networking.
- It is a P/Invoke wrapper around a native C library, delivering high performance through a native implementation.
- The library exposes the exact same public API as NativeSockets, making it a drop‑in replacement.
- Precompiled native binaries are included for all supported platforms and architectures.
- Windows
- Linux
- macOS
- iOS
- Android
- tvOS
- watchOS
- visionOS
- Zero garbage collection pressure
- Same API as NativeSockets – seamless switching between managed and native backends.
- Complete UDP support – create, bind, connect, send, receive, and poll sockets.
- Ipv4 and Ipv6 with dual‑mode support.
- Allocation‑free extension methods for
System.Net.Sockets.Socket:SendTo– send data without temporary allocations.ReceiveFrom– receive data and capture the remote endpoint without allocations.
- Scatter/gather I/O – Efficient vectored send and receive operations.
- The NuGet package includes the native C library for all supported platforms.
- The correct binary is loaded automatically at runtime – no extra installation or configuration is needed.
- Just add the package and use the same API as you would with the original NativeSockets.
- You can build binaries using GitHub Actions.
A unified socket type that automatically picks the best available backend at runtime:
- When
NativeSocketPal.IsSupportedistrue,VirtualSocketis backed by the native socket implementation. - Otherwise, it transparently falls back to
System.Net.Sockets.Socket.
- No manual
NativeSocketPal.Startup/NativeSocketPal.Cleanupcalls are required – the underlying socket subsystem is managed automatically. - The send/receive API mirrors the native socket API and returns an
IoResult(byte count plusSocketError). SocketFlagsare restricted: send operations honor onlyNoneandDontRoute; receive operations honor onlyNoneandPeek. Any other flags are silently ignored.- For vectored receive operations, truncated data is reported as
IoResult.ErrwithSocketError.MessageSize, even when the underlying operation succeeded.
- On the managed fallback path, a
SendTothat triggers an implicit bind leaves the socket actually bound, butLocalEndPointcannot be queried and throws. This only occurs on .NET 8 and later, because theSendTo(ReadOnlySpan<byte>, SocketFlags, SocketAddress)overload does not set the underlying_rightEndPointfield. In that stateGetNamereports an error even though the datagram was delivered. - The managed fallback path carries the overhead of
System.Net.Sockets; prefer the native backend when performance matters. - When
NativeSocketPal.IsSupportedisfalseand the socket is non-blocking, consider polling withPollorPollFlagsbefore sending or receiving, because frequentSocketError.WouldBlockexceptions have a severe impact on performance.