mbed_example
/
DNS_GetHostbyNameAsync_Example
An example how to make asynchronous DNS host name resolution.
Revision 0:d0f7d306a900, committed 2018-05-25
- Comitter:
- mikaleppanen
- Date:
- Fri May 25 12:25:32 2018 +0000
- Commit message:
- Example how to make asynchronous DNS get host by name operation
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed-os.lib | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri May 25 12:25:32 2018 +0000 @@ -0,0 +1,38 @@ +#include "mbed.h" +#include "nsapi_types.h" +#include "EthernetInterface.h" +#include "SocketAddress.h" +#include "Semaphore.h" + +rtos::Semaphore callback_semaphore; +SocketAddress address; +nsapi_error_t result; + +// Callback for asynchronous host name resolution +void hostbyname_callback(nsapi_error_t res, SocketAddress *addr) +{ + // Store result and release semaphore + result = res; + address = *addr; + callback_semaphore.release(); +} + +int main() +{ + // Initialise network interface + EthernetInterface eth; + eth.connect(); + + // Initiate asynchronous DNS host name resolution + eth.gethostbyname_async("www.mbed.com", hostbyname_callback); + + // Wait for callback semaphore + callback_semaphore.wait(); + + // Print result + printf("Result %s, Address %s\r\n", result == NSAPI_ERROR_OK ? "OK" : "FAIL", + result == NSAPI_ERROR_OK ? address.get_ip_address() : "NONE"); + + // Disconnect network interface + eth.disconnect(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Fri May 25 12:25:32 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#c26126ee5951b70703989c844c4d0bc8f498049b