Ethernet Interface example coding

Files at this revision

API Documentation at this revision

Comitter:
mab5449
Date:
Thu Jan 19 11:46:10 2017 -0600
Child:
1:965d7fb768b6
Commit message:
Ported mbed OS 2 to mbed OS 5

Changed in this revision

README.md Show annotated file Show diff for this revision Revisions of this file
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
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Thu Jan 19 11:46:10 2017 -0600
@@ -0,0 +1,15 @@
+### 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.
+
+### 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/5.1/APIs/communication/network_sockets/).
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jan 19 11:46:10 2017 -0600
@@ -0,0 +1,38 @@
+#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();
+    printf("IP address is: %s\n", ip ? ip : "No IP");
+
+    // Open a socket on the network interface, and create a TCP connection to mbed.org
+    TCPSocket socket;
+    socket.open(&net);
+    socket.connect("developer.mbed.org", 80);
+
+    // Send a simple http request
+    char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
+    int scount = socket.send(sbuffer, sizeof sbuffer);
+    printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+
+    // Recieve a simple http response and print out the response line
+    char rbuffer[64];
+    int rcount = socket.recv(rbuffer, sizeof rbuffer);
+    printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
+
+    // Close the socket to return its memory and bring down the network interface
+    socket.close();
+
+    // Bring down the ethernet interface
+    net.disconnect();
+    printf("Done\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Jan 19 11:46:10 2017 -0600
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#34c1facf42a174f47fdf9002cd8c6bf10ac41744
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Jan 19 11:46:10 2017 -0600
@@ -0,0 +1,7 @@
+{
+    "target_overrides": {
+        "UBLOX_EVK_ODIN_W2": {
+            "target.device_has_remove": ["EMAC"]
+        }
+    }
+}