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


Use the SocketAddress class to represent the IP address and port pair of a unique network endpoint. Most network functions are also overloaded to accept string representations of IP addresses, but you can use SocketAddress to avoid the overhead of parsing IP addresses during repeated network transactions, and you can pass it around as a first class object.

SocketAddress class reference

Public Member Functions
template<typename S >
 SocketAddress (S *stack, const char *host, uint16_t port=0)
 SocketAddress (nsapi_addr_t addr=nsapi_addr_t(), uint16_t port=0)
 SocketAddress (const char *addr, uint16_t port=0)
 SocketAddress (const void *bytes, nsapi_version_t version, uint16_t port=0)
 SocketAddress (const SocketAddress &addr)
bool set_ip_address (const char *addr)
void set_ip_bytes (const void *bytes, nsapi_version_t version)
void set_addr (nsapi_addr_t addr)
void set_port (uint16_t port)
const char * get_ip_address () const
const void * get_ip_bytes () const
nsapi_version_t get_ip_version () const
nsapi_addr_t get_addr () const
uint16_t get_port () const
 operator bool () const
bool operator== (const SocketAddress &a, const SocketAddress &b)
bool operator!= (const SocketAddress &a, const SocketAddress &b)

SocketAddress Example

Here is an example to read current UTC time. This example uses the SocketAddress class to get the server IP address and port.

#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.