Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:d0f7d306a900, 2018-05-25 (annotated)
- Committer:
- mikaleppanen
- Date:
- Fri May 25 12:25:32 2018 +0000
- Revision:
- 0:d0f7d306a900
Example how to make asynchronous DNS get host by name operation
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mikaleppanen | 0:d0f7d306a900 | 1 | #include "mbed.h" |
| mikaleppanen | 0:d0f7d306a900 | 2 | #include "nsapi_types.h" |
| mikaleppanen | 0:d0f7d306a900 | 3 | #include "EthernetInterface.h" |
| mikaleppanen | 0:d0f7d306a900 | 4 | #include "SocketAddress.h" |
| mikaleppanen | 0:d0f7d306a900 | 5 | #include "Semaphore.h" |
| mikaleppanen | 0:d0f7d306a900 | 6 | |
| mikaleppanen | 0:d0f7d306a900 | 7 | rtos::Semaphore callback_semaphore; |
| mikaleppanen | 0:d0f7d306a900 | 8 | SocketAddress address; |
| mikaleppanen | 0:d0f7d306a900 | 9 | nsapi_error_t result; |
| mikaleppanen | 0:d0f7d306a900 | 10 | |
| mikaleppanen | 0:d0f7d306a900 | 11 | // Callback for asynchronous host name resolution |
| mikaleppanen | 0:d0f7d306a900 | 12 | void hostbyname_callback(nsapi_error_t res, SocketAddress *addr) |
| mikaleppanen | 0:d0f7d306a900 | 13 | { |
| mikaleppanen | 0:d0f7d306a900 | 14 | // Store result and release semaphore |
| mikaleppanen | 0:d0f7d306a900 | 15 | result = res; |
| mikaleppanen | 0:d0f7d306a900 | 16 | address = *addr; |
| mikaleppanen | 0:d0f7d306a900 | 17 | callback_semaphore.release(); |
| mikaleppanen | 0:d0f7d306a900 | 18 | } |
| mikaleppanen | 0:d0f7d306a900 | 19 | |
| mikaleppanen | 0:d0f7d306a900 | 20 | int main() |
| mikaleppanen | 0:d0f7d306a900 | 21 | { |
| mikaleppanen | 0:d0f7d306a900 | 22 | // Initialise network interface |
| mikaleppanen | 0:d0f7d306a900 | 23 | EthernetInterface eth; |
| mikaleppanen | 0:d0f7d306a900 | 24 | eth.connect(); |
| mikaleppanen | 0:d0f7d306a900 | 25 | |
| mikaleppanen | 0:d0f7d306a900 | 26 | // Initiate asynchronous DNS host name resolution |
| mikaleppanen | 0:d0f7d306a900 | 27 | eth.gethostbyname_async("www.mbed.com", hostbyname_callback); |
| mikaleppanen | 0:d0f7d306a900 | 28 | |
| mikaleppanen | 0:d0f7d306a900 | 29 | // Wait for callback semaphore |
| mikaleppanen | 0:d0f7d306a900 | 30 | callback_semaphore.wait(); |
| mikaleppanen | 0:d0f7d306a900 | 31 | |
| mikaleppanen | 0:d0f7d306a900 | 32 | // Print result |
| mikaleppanen | 0:d0f7d306a900 | 33 | printf("Result %s, Address %s\r\n", result == NSAPI_ERROR_OK ? "OK" : "FAIL", |
| mikaleppanen | 0:d0f7d306a900 | 34 | result == NSAPI_ERROR_OK ? address.get_ip_address() : "NONE"); |
| mikaleppanen | 0:d0f7d306a900 | 35 | |
| mikaleppanen | 0:d0f7d306a900 | 36 | // Disconnect network interface |
| mikaleppanen | 0:d0f7d306a900 | 37 | eth.disconnect(); |
| mikaleppanen | 0:d0f7d306a900 | 38 | } |