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 | |
template<typename S > | |
SocketAddress (S *stack, const char *host, uint16_t port=0) | |
Create a SocketAddress from a hostname and port. More... | |
SocketAddress (nsapi_addr_t addr=nsapi_addr_t(), 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 () | |
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 (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... | |
SocketAddress & | operator= (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.
#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.
}ntp_packet;
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",
(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;
}