Mistake on this page?
Report an issue in GitHub or email us


UDPSocket class hierarchy

The UDPSocket class provides the ability to send packets of data over UDP, using the sendto and recvfrom member functions. Packets can be lost or arrive out of order, so we suggest using a TCPSocket when you require guaranteed delivery.

The constructor takes in the NetworkStack pointer to open the socket on the specified NetworkInterface. If you do not pass in the constructor, then you must call open to initialize the socket.

UDPSocket class reference

Public Member Functions
 UDPSocket ()
template<typename S >
 UDPSocket (S *stack)
virtual ~UDPSocket ()
nsapi_size_or_error_t sendto (const char *host, uint16_t port, const void *data, nsapi_size_t size)
nsapi_size_or_error_t sendto (const SocketAddress &address, const void *data, nsapi_size_t size)
nsapi_size_or_error_t recvfrom (SocketAddress *address, void *data, nsapi_size_t size)
 Public Member Functions inherited from Socket
virtual ~Socket ()
nsapi_error_t open (NetworkStack *stack)
template<typename S >
nsapi_error_t open (S *stack)
nsapi_error_t close ()
int join_multicast_group (const SocketAddress &address)
int leave_multicast_group (const SocketAddress &address)
nsapi_error_t bind (uint16_t port)
nsapi_error_t bind (const char *address, uint16_t port)
nsapi_error_t bind (const SocketAddress &address)
void set_blocking (bool blocking)
void set_timeout (int timeout)
nsapi_error_t setsockopt (int level, int optname, const void *optval, unsigned optlen)
nsapi_error_t getsockopt (int level, int optname, void *optval, unsigned *optlen)
void sigio (mbed::Callback< void()> func)
void attach (mbed::Callback< void()> func)
template<typename T , typename M >
void attach (T *obj, M method)
Protected Member Functions
virtual nsapi_protocol_t get_proto ()
virtual void event ()
 Protected Member Functions inherited from Socket
int modify_multicast_group (const SocketAddress &address, nsapi_socket_option_t socketopt)
Protected Attributes
volatile unsigned _pending
rtos::EventFlags _event_flag
 Protected Attributes inherited from Socket
nsapi_socket_t _socket
uint32_t _timeout
mbed::Callback< void()> _event
mbed::Callback< void()> _callback
rtos::Mutex _lock

UDPSocket Example

Here is a UDP example to read the current UTC time by sending a request to the NIST Internet Time Service.

#include "mbed.h"
#include "EthernetInterface.h"
// Network interface
EthernetInterface net;
// Time protocol implementation : Address: time.nist.gov UDPPort: 37  
typedef struct {
    uint32_t secs;         // Transmit Time-stamp seconds.
int main() {
    // Bring up the ethernet interface
    printf("UDP Socket example\n");
    if(0 != net.connect()) {
        printf("Error connecting\n");
        return -1;
    // Show the network address
    const char *ip = net.get_ip_address();
    printf("IP address is: %s\n", ip ? ip : "No IP");
    UDPSocket sock(&net);
    SocketAddress sockAddr;
    char out_buffer[] = "time";
    if(0 > sock.sendto("time.nist.gov", 37, out_buffer, sizeof(out_buffer))) {
        printf("Error sending data\n");
        return -1;
    ntp_packet in_data;
    int n = sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet));
    in_data.secs = ntohl( in_data.secs ) - 2208988800;    // 1900-1970
    printf("Time Received %lu seconds since 1/01/1900 00:00 GMT\n", 
    printf("Time = %s", ctime(( const time_t* )&in_data.secs));
    printf("Time Server Address: %s Port: %d\n\r", 
                               sockAddr.get_ip_address(), sockAddr.get_port());
    // Close the socket and bring down the network interface
    return 0;
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.