xiao sun
/
mbed-os-example-sockets
NS example
Revision 0:f40a402e1725, committed 2017-05-02
- Comitter:
- sunsmile2015
- Date:
- Tue May 02 15:29:27 2017 +0000
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue May 02 15:29:27 2017 +0000 @@ -0,0 +1,31 @@ +### Getting started with the network-socket API ### + +This is a quick example of a simple HTTP client program using the +network-socket API that is provided as a part of [mbed-os](github.com/armmbed/mbed-os). + +The program brings up an underlying network interface, and uses it to perform an HTTP +transaction over a TCPSocket. + +**Note:** The current example is limited to the ethernet interface on supported devices. +To use the example with a different interface, you will need to modify main.cpp and +replace the EthernetInterface class with the appropriate interface. + +### Expected output ### + +``` +IP address: 10.118.14.45 +Netmask: 255.255.252.0 +Gateway: 10.118.12.1 +sent 39 [GET / HTTP/1.1] +recv 173 [HTTP/1.1 200 OK] +External IP address: 217.140.111.135 +Done +``` + +### Documentation ### + +More information on the network-socket API can be found in the [mbed handbook](https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/communication/network_sockets/). + +### Known issues + +- ARCH_PRO runtime fails for all toolchains - issue [here](https://github.com/ARMmbed/mbed-os-example-sockets/issues/17)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue May 02 15:29:27 2017 +0000 @@ -0,0 +1,52 @@ +#include "mbed.h" +#include "EthernetInterface.h" + +// Network interface +EthernetInterface net; + +// Socket demo +int main() { + // Bring up the ethernet interface + printf("Ethernet socket example\n"); + net.connect(); + + // Show the network address + const char *ip = net.get_ip_address(); + const char *netmask = net.get_netmask(); + const char *gateway = net.get_gateway(); + printf("IP address: %s\n", ip ? ip : "None"); + printf("Netmask: %s\n", netmask ? netmask : "None"); + printf("Gateway: %s\n", gateway ? gateway : "None"); + + // Open a socket on the network interface, and create a TCP connection to mbed.org + TCPSocket socket; +//UDPSocket udp; +//udp.open(&net); +//udp.bind(56830); +//udp.sendto(); +//udp.recvfrom(); + socket.open(&net); + socket.connect("api.ipify.org", 80); + char *buffer = new char[256]; + + // Send an HTTP request + strcpy(buffer, "GET / HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n"); + int scount = socket.send(buffer, strlen(buffer)); + printf("sent %d [%.*s]\n", scount, strstr(buffer, "\r\n")-buffer, buffer); + + // Recieve an HTTP response and print out the response line + int rcount = socket.recv(buffer, 256); + printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer); + + // The api.ipify.org service also gives us the device's external IP address + const char *payload = strstr(buffer, "\r\n\r\n")+4; + printf("External IP address: %.*s\n", rcount-(payload-buffer), payload); + + // Close the socket to return its memory and bring down the network interface + socket.close(); + delete[] buffer; + + // Bring down the ethernet interface + net.disconnect(); + printf("Done\n"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue May 02 15:29:27 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#ed4febefdede4e88743ca12909b7bc9a3993889a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Tue May 02 15:29:27 2017 +0000 @@ -0,0 +1,7 @@ +{ + "target_overrides": { + "UBLOX_EVK_ODIN_W2": { + "target.device_has_remove": ["EMAC"] + } + } +}