tcp socket client

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPSocket_HelloWorld by Seeed

Committer:
yihui
Date:
Thu Apr 23 02:01:38 2015 +0000
Revision:
17:0d74817db362
Parent:
16:bfcf5f6274a2
Child:
18:2eaa23c78a5b
add mbed_mac_address() to use a specific mac address

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:bb128f0e952f 1 #include "mbed.h"
donatien 0:bb128f0e952f 2 #include "EthernetInterface.h"
donatien 0:bb128f0e952f 3
emilmont 7:65188f4a8c25 4 int main() {
donatien 0:bb128f0e952f 5 EthernetInterface eth;
donatien 0:bb128f0e952f 6 eth.init(); //Use DHCP
donatien 0:bb128f0e952f 7 eth.connect();
emilmont 2:e087e9b789e9 8 printf("IP Address is %s\n", eth.getIPAddress());
donatien 0:bb128f0e952f 9
emilmont 7:65188f4a8c25 10 TCPSocketConnection sock;
yihui 16:bfcf5f6274a2 11 sock.connect("httpbin.org", 80);
donatien 0:bb128f0e952f 12
yihui 16:bfcf5f6274a2 13 char http_cmd[] = "GET /get?helloworld HTTP/1.0\r\n\r\n";
emilmont 11:59dcefdda506 14 sock.send_all(http_cmd, sizeof(http_cmd)-1);
emilmont 7:65188f4a8c25 15
emilmont 9:4757a976148d 16 char buffer[300];
donatien 0:bb128f0e952f 17 int ret;
emilmont 7:65188f4a8c25 18 while (true) {
emilmont 9:4757a976148d 19 ret = sock.receive(buffer, sizeof(buffer)-1);
emilmont 7:65188f4a8c25 20 if (ret <= 0)
emilmont 7:65188f4a8c25 21 break;
emilmont 9:4757a976148d 22 buffer[ret] = '\0';
emilmont 9:4757a976148d 23 printf("Received %d chars from server:\n%s\n", ret, buffer);
emilmont 7:65188f4a8c25 24 }
donatien 0:bb128f0e952f 25
emilmont 7:65188f4a8c25 26 sock.close();
donatien 0:bb128f0e952f 27
emilmont 7:65188f4a8c25 28 eth.disconnect();
donatien 5:01f6c3e112af 29
emilmont 9:4757a976148d 30 while(1) {}
donatien 0:bb128f0e952f 31 }
yihui 17:0d74817db362 32
yihui 17:0d74817db362 33 // override the default weak function to provide a specific mac address
yihui 17:0d74817db362 34 extern "C" void mbed_mac_address(char *mac)
yihui 17:0d74817db362 35 {
yihui 17:0d74817db362 36 mac[0] = 0x01;
yihui 17:0d74817db362 37 mac[1] = 0x23;
yihui 17:0d74817db362 38 mac[2] = 0x45;
yihui 17:0d74817db362 39 mac[3] = 0x67;
yihui 17:0d74817db362 40 mac[4] = 0x89;
yihui 17:0d74817db362 41 mac[5] = 0xAB;
yihui 17:0d74817db362 42 };