boost::corosio::udp_socket
An asynchronous UDP socket for coroutine I/O.
Synopsis
Declared in <boost/corosio/udp_socket.hpp>
class udp_socket
: public io_object
Description
This class provides asynchronous UDP datagram operations that return awaitable types. Each operation participates in the affine awaitable protocol, ensuring coroutines resume on the correct executor.
Supports two modes of operation:
*Connectionless mode**: each send_to specifies a destination endpoint, and each recv_from captures the source endpoint. The socket must be opened (and optionally bound) before I/O.
*Connected mode**: call connect() to set a default peer, then use send()/`recv()` without endpoint arguments. The kernel filters incoming datagrams to those from the connected peer.
Thread Safety
Distinct objects: Safe. Shared objects: Unsafe. A socket must not have concurrent operations of the same type (e.g., two simultaneous recv_from). One send_to and one recv_from may be in flight simultaneously.
Example
// Connectionless mode
io_context ioc;
udp_socket sock( ioc );
sock.open( udp::v4() );
sock.bind( endpoint( ipv4_address::any(), 9000 ) );
char buf[1024];
endpoint sender;
auto [ec, n] = co_await sock.recv_from(
capy::mutable_buffer( buf, sizeof( buf ) ), sender );
if ( !ec )
co_await sock.send_to(
capy::const_buffer( buf, n ), sender );
// Connected mode
udp_socket csock( ioc );
auto [cec] = co_await csock.connect(
endpoint( ipv4_address::loopback(), 9000 ) );
if ( !cec )
co_await csock.send(
capy::const_buffer( buf, n ) );
Types
Name |
Description |
Represent the awaitable returned by |
|
RAII wrapper for I/O object implementation lifetime. |
|
Define backend hooks for UDP socket operations. |
|
Service interface for I/O object lifecycle management. |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
|
Represent the awaitable returned by |
Member Functions
Name |
Description |
|
Constructors |
|
Destructor. |
|
Move assignment operator. |
Bind the socket to a local endpoint. |
|
Cancel any pending asynchronous operations. |
|
Close the socket. |
|
Initiate an asynchronous connect to set the default peer. |
|
Return the execution context. |
|
Get a socket option. |
|
Check if the socket is open. |
|
Get the local endpoint of the socket. |
|
Get the native socket handle. |
|
Open the socket. |
|
Receive a datagram from the connected peer. |
|
Receive a datagram and capture the sender's endpoint. |
|
Get the remote endpoint of the socket. |
|
Send a datagram to the connected peer. |
|
Send a datagram to the specified destination. |
|
Set a socket option. |
|
Wait for the socket to become ready in a given direction. |
Protected Member Functions
Name |
Description |
|
Construct from a pre‐built handle (for native_udp_socket). |
|
Move assign from another I/O object. |
Protected Static Member Functions
Name |
Description |
Create a handle bound to a service found in the context. |
Derived Classes
Name |
Description |
An asynchronous UDP socket with devirtualized I/O operations. |
Created with MrDocs