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

SocketAddress

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
constexpr SocketAddress ()=default
 Create an unspecified SocketAddress. More...
 SocketAddress (const nsapi_addr_t &addr, uint16_t port=0)
 Create a SocketAddress from a raw IP address and port. More...
 SocketAddress (const char *addr, uint16_t port=0)
 Create a SocketAddress from an IP address and port. More...
 SocketAddress (const void *bytes, nsapi_version_t version, uint16_t port=0)
 Create a SocketAddress from raw IP bytes, IP version, and port. More...
 SocketAddress (const SocketAddress &addr)
 Create a SocketAddress from another SocketAddress. More...
 ~SocketAddress ()=default
 Destructor. More...
bool set_ip_address (const char *addr)
 Set the IP address. More...
void set_ip_bytes (const void *bytes, nsapi_version_t version)
 Set the raw IP bytes and IP version. More...
void set_addr (const nsapi_addr_t &addr)
 Set the raw IP address. More...
void set_port (uint16_t port)
 Set the port. More...
const char * get_ip_address () const
 Get the human-readable IP address. More...
const void * get_ip_bytes () const
 Get the raw IP bytes. More...
nsapi_version_t get_ip_version () const
 Get the IP address version. More...
nsapi_addr_t get_addr () const
 Get the raw IP address. More...
uint16_t get_port () const
 Get the port. More...
 operator bool () const
 Test if address is zero. More...
SocketAddressoperator= (const SocketAddress &addr)
 Copy address from another SocketAddress. More...
Friends
bool operator== (const SocketAddress &a, const SocketAddress &b)
 Compare two addresses for equality. More...
bool operator!= (const SocketAddress &a, const SocketAddress &b)
 Compare two addresses for equality. More...

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.

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"
#include "EthernetInterface.h"
#include "LWIPStack.h"

// Network interface
EthernetInterface net;

// Time protocol implementation : Address: time.nist.gov UDPPort: 37

typedef struct {
    uint32_t secs;         // Transmit Time-stamp seconds.
} ntp_packet;

int main()
{
    SocketAddress sockAddr;

    // Bring up the ethernet interface
    printf("UDP Socket example\n");
    if (0 != net.connect()) {
        printf("Error connecting\n");
        return -1;
    }

    // Show the network address
    net.get_ip_address(&sockAddr);
    printf("IP address is: %s\n", sockAddr.get_ip_address() ? sockAddr.get_ip_address() : "No IP");

    UDPSocket sock;
    sock.open(&net);

    net.gethostbyname("time.nist.gov", &sockAddr);
    sockAddr.set_port(37);

    char out_buffer[] = "time";
    if (0 > sock.sendto(sockAddr, out_buffer, sizeof(out_buffer))) {
        printf("Error sending data\n");
        return -1;
    }

    ntp_packet in_data;
    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",
           (uint32_t)in_data.secs);
    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
    sock.close();
    net.disconnect();
    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.