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.
Dependencies: wave_player 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library
Diff: main.cpp
- Revision:
- 64:a3dc04daaa2a
- Parent:
- 58:4aeb931b8475
- Child:
- 72:47ae7d8d7321
diff -r 6b43e0875dbc -r a3dc04daaa2a main.cpp
--- a/main.cpp Mon Oct 22 09:15:04 2018 +0100
+++ b/main.cpp Wed Oct 24 15:45:03 2018 +0100
@@ -9,7 +9,7 @@
int rcount;
char *p;
char *buffer = new char[256];
- nsapi_size_or_error_t r;
+ nsapi_size_or_error_t result;
// Bring up the ethernet interface
printf("Mbed OS Socket example\n");
@@ -25,10 +25,10 @@
return 0;
}
- r = net->connect();
- if (r != 0) {
- printf("Error! net->connect() returned: %d\n", r);
- return r;
+ result = net->connect();
+ if (result != 0) {
+ printf("Error! net->connect() returned: %d\n", result);
+ return result;
}
// Show the network address
@@ -39,48 +39,48 @@
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
+ // Open a socket on the network interface, and create a TCP connection to api.ipify.org
TCPSocket socket;
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
nsapi_size_t size = strlen(sbuffer);
- r = socket.open(net);
- if (r != 0) {
- printf("Error! socket.open() returned: %d\n", r);
+ result = socket.open(net);
+ if (result != 0) {
+ printf("Error! socket.open() returned: %d\n", result);
}
- r = socket.connect("api.ipify.org", 80);
- if (r != 0) {
- printf("Error! socket.connect() returned: %d\n", r);
+ result = socket.connect("api.ipify.org", 80);
+ if (result != 0) {
+ printf("Error! socket.connect() returned: %d\n", result);
goto DISCONNECT;
}
- // Loop until whole request send
+ // Loop until whole request sent
while(size) {
- r = socket.send(sbuffer+r, size);
- if (r < 0) {
- printf("Error! socket.send() returned: %d\n", r);
+ result = socket.send(sbuffer+result, size);
+ if (result < 0) {
+ printf("Error! socket.send() returned: %d\n", result);
goto DISCONNECT;
}
- size -= r;
- printf("sent %d [%.*s]\n", r, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+ size -= result;
+ printf("sent %d [%.*s]\n", result, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
}
// Receieve an HTTP response and print out the response line
remaining = 256;
rcount = 0;
p = buffer;
- while (0 < (r = socket.recv(p, remaining))) {
- p += r;
- rcount += r;
- remaining -= r;
+ while (0 < (result = socket.recv(p, remaining))) {
+ p += result;
+ rcount += result;
+ remaining -= result;
}
- if (r < 0) {
- printf("Error! socket.recv() returned: %d\n", r);
+ if (result < 0) {
+ printf("Error! socket.recv() returned: %d\n", result);
goto DISCONNECT;
}
-
+ // the HTTP response code
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