forked from ARMmbed/mbed-os-example-sockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
95 lines (79 loc) · 2.64 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "mbed.h"
// Network interface
NetworkInterface *net;
// Socket demo
int main() {
int remaining;
int rcount;
char *p;
char *buffer = new char[256];
nsapi_size_or_error_t result;
// Bring up the ethernet interface
printf("Mbed OS Socket example\n");
#ifdef MBED_MAJOR_VERSION
printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
net = NetworkInterface::get_default_instance();
if (!net) {
printf("Error! No network inteface found.\n");
return 0;
}
result = net->connect();
if (result != 0) {
printf("Error! net->connect() returned: %d\n", result);
return result;
}
// 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 ifconfig.io
TCPSocket socket;
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\nConnection: close\r\n\r\n";
nsapi_size_t size = strlen(sbuffer);
result = socket.open(net);
if (result != 0) {
printf("Error! socket.open() returned: %d\n", result);
}
result = socket.connect("ifconfig.io", 80);
if (result != 0) {
printf("Error! socket.connect() returned: %d\n", result);
goto DISCONNECT;
}
// Loop until whole request sent
while(size) {
result = socket.send(sbuffer+result, size);
if (result < 0) {
printf("Error! socket.send() returned: %d\n", result);
goto DISCONNECT;
}
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 (remaining > 0 && 0 < (result = socket.recv(p, remaining))) {
p += result;
rcount += result;
remaining -= result;
}
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);
delete[] buffer;
DISCONNECT:
// 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");
}