udp_socket


An os_udp_socket provides unreliable, connectionless communication. A datagram is a single packet of information that is sent from one socket to another. The sender must know the address of the destination socket. Because datagram communication is unreliable, there is no guarantee that the receiver will actually get the datagram.

Although datagram communication is connectionless, a UDP socket can be connected to a destination address. If you call connect_to() in a UDP socket, all datagrams that are subsequently sent using send() are automatically routed to the connected address. Each use of connect_to() overrides the previous connection address.

Library

Network<ToolKit>

Declaration

#include <ospace/socket/udpsock.h>

class os_udp_socket : public os_connectable_socket

Enums

enum os_udp_socket::bind_t
  {
  bound,
  unbound
  };
  

Interface

Constructor
os_udp_socket( bind_t bound )
If bound is os_udp_socket::bound (default), constructs a UDP socket with a unique socket address; otherwise, constructs an unbound socket.

Throws: os_network_toolkit_error

Constructor
os_udp_socket( const os_socket_address& address )
Constructs a socket that is a new datagram socket bound to the IP socket address address .

Throws: os_network_toolkit_error

Constructor
os_udp_socket( os_sock_t descriptor )
Constructs a socket based on the native operating system socket descriptor .

Throws: os_network_toolkit_error

broadcast
bool broadcast() const
Returns true if broadcasting is enabled.

Throws: os_network_toolkit_error

broadcast
void broadcast( bool flag )
Sets broadcast enabling to flag .

Throws: os_network_toolkit_error

join_multicast_group
void join_multicast_group( const os_ip_address& group_address , const os_ip_address& local_interface )
Joins the multicast group group_address . group_address  must be a CLASS-D IP multicast address. Addresses in the range "224.0.0.0" through "239.255.255.255" are multicast addresses. The local interface local_interface (default os_ip_address() which is INADDR_ANY) is used as the interface address for incoming multicast datagrams. The default local_interface causes the kernel to choose the interface to join on. More than one join is allowed but each join must specify a different group address or a same group address but a different local_interface (for multihomed host).

Throws: os_network_toolkit_error

leave_multicast_group
void leave_multicast_group( const os_ip_address& group_address , const os_ip_address& local_interface )
Leaves the multicast group group_address  on the specified local interface local_interface . It should be same as the one specified when joining the multicast group using join_multi_group() function.

Throws: os_network_toolkit_error

multicast_interface
os_ip_address multicast_interface()
Returns the current multicast local interface.

Throws: os_network_toolkit_error

multicast_interface
void multicast_interface( const os_ip_address& local_interface )
Sets the multicast local interface. local_interface specifies the outgoing interface for multicast packets. If local_interface is a default constructed os_ip_address object then the local address is INADDR_ANY and the kernel will use the unicast routing table to select the outgoing interface.

Throws: os_network_toolkit_error

multicast_loopback
void multicast_loopback( bool flag )
Enables or disables multicast loopback. By default, multicast loopback is enabled so that multicast datagrams are delivered to members of on the sending host. If flag is false, multicast loop back is disabled.

Throws: os_network_toolkit_error

multicast_time_to_live
unsigned char multicast_time_to_live() const
Returns the current multicast time-to-live (TTL) value.

Throws: os_network_toolkit_error

multicast_time_to_live
void multicast_time_to_live( unsigned char ttl )
Sets the time-to-live (TTL) for outgoing multicast datagrams to ttl . The default value is 1.

Throws: os_network_toolkit_error

receive_from
ssize_t receive_from( os_socket_address& address , void* buffer , size_t bytes )
Receives up to the specified number of bytes of data into buffer from the socket. Returns the number of bytes that are successfully read. Sets address to the IP socket address of the socket that contains the bytes read. [not MTSAFE for Solaris]

Throws: os_network_toolkit_error

reset
void reset()
Destroys the existing underlying socket and replaces it with a newly-constructed socket.

Throws: os_network_toolkit_error

send_to
ssize_t send_to( const os_socket_address& address , const void* buffer , size_t bytes )
Sends up to the specified number of bytes from buffer to the socket with IP socket address address . Returns the number of bytes successfully written or returns or returns -1 on error. [not MTSAFE on Solaris]

Throws: os_network_toolkit_error


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.