4180 LabTeam - Spring '18 / Mbed 2 deprecated Lab2_Part4

Dependencies:   EthernetInterface mbed-rtos mbed

Committer:
bjs9
Date:
Mon Feb 12 21:18:19 2018 +0000
Revision:
0:ceed50ae31a8
Child:
1:c615a1ddf092
lab2_part4;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjs9 0:ceed50ae31a8 1 #include "mbed.h"
bjs9 0:ceed50ae31a8 2 #include "EthernetInterface.h"
bjs9 0:ceed50ae31a8 3
bjs9 0:ceed50ae31a8 4 int main() {
bjs9 0:ceed50ae31a8 5 EthernetInterface eth;
bjs9 0:ceed50ae31a8 6 eth.init(); //Use DHCP
bjs9 0:ceed50ae31a8 7 eth.connect();
bjs9 0:ceed50ae31a8 8 printf("IP Address is %s\n", eth.getIPAddress());
bjs9 0:ceed50ae31a8 9
bjs9 0:ceed50ae31a8 10 TCPSocketConnection sock;
bjs9 0:ceed50ae31a8 11 sock.connect("mbed.org", 80);
bjs9 0:ceed50ae31a8 12
bjs9 0:ceed50ae31a8 13 char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
bjs9 0:ceed50ae31a8 14 sock.send_all(http_cmd, sizeof(http_cmd)-1);
bjs9 0:ceed50ae31a8 15
bjs9 0:ceed50ae31a8 16 char buffer[300];
bjs9 0:ceed50ae31a8 17 int ret;
bjs9 0:ceed50ae31a8 18 while (true) {
bjs9 0:ceed50ae31a8 19 ret = sock.receive(buffer, sizeof(buffer)-1);
bjs9 0:ceed50ae31a8 20 if (ret <= 0)
bjs9 0:ceed50ae31a8 21 break;
bjs9 0:ceed50ae31a8 22 buffer[ret] = '\0';
bjs9 0:ceed50ae31a8 23 printf("Received %d chars from server:\n%s\n", ret, buffer);
bjs9 0:ceed50ae31a8 24 }
bjs9 0:ceed50ae31a8 25
bjs9 0:ceed50ae31a8 26 sock.close();
bjs9 0:ceed50ae31a8 27
bjs9 0:ceed50ae31a8 28 eth.disconnect();
bjs9 0:ceed50ae31a8 29
bjs9 0:ceed50ae31a8 30 while(1) {}
bjs9 0:ceed50ae31a8 31 }