1
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@0:ea9c19e28331, 2016-09-08 (annotated)
- Committer:
- yu10078999
- Date:
- Thu Sep 08 08:21:49 2016 +0000
- Revision:
- 0:ea9c19e28331
- Child:
- 1:b1506df02fe6
0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yu10078999 | 0:ea9c19e28331 | 1 | #include "mbed.h" |
yu10078999 | 0:ea9c19e28331 | 2 | #include "EthernetInterface.h" |
yu10078999 | 0:ea9c19e28331 | 3 | |
yu10078999 | 0:ea9c19e28331 | 4 | int main() { |
yu10078999 | 0:ea9c19e28331 | 5 | const char *ip = "0.0.0.0"; // ip |
yu10078999 | 0:ea9c19e28331 | 6 | const char *mask =""; // 遮罩 |
yu10078999 | 0:ea9c19e28331 | 7 | const char *gateway=""; // 閘道器 |
yu10078999 | 0:ea9c19e28331 | 8 | EthernetInterface eth; |
yu10078999 | 0:ea9c19e28331 | 9 | eth.init(ip,mask,gateway); //Use static ip |
yu10078999 | 0:ea9c19e28331 | 10 | //eth.init(); //Use DHCP |
yu10078999 | 0:ea9c19e28331 | 11 | eth.connect(); |
yu10078999 | 0:ea9c19e28331 | 12 | printf("IP Address is %s\n", eth.getIPAddress()); |
yu10078999 | 0:ea9c19e28331 | 13 | |
yu10078999 | 0:ea9c19e28331 | 14 | TCPSocketConnection sock; |
yu10078999 | 0:ea9c19e28331 | 15 | sock.connect("httpbin.org", 80); |
yu10078999 | 0:ea9c19e28331 | 16 | |
yu10078999 | 0:ea9c19e28331 | 17 | char http_cmd[] = "GET /get?helloworld HTTP/1.0\r\n\r\n"; |
yu10078999 | 0:ea9c19e28331 | 18 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
yu10078999 | 0:ea9c19e28331 | 19 | |
yu10078999 | 0:ea9c19e28331 | 20 | char buffer[300]; |
yu10078999 | 0:ea9c19e28331 | 21 | int ret; |
yu10078999 | 0:ea9c19e28331 | 22 | while (true) { |
yu10078999 | 0:ea9c19e28331 | 23 | ret = sock.receive(buffer, sizeof(buffer)-1); |
yu10078999 | 0:ea9c19e28331 | 24 | if (ret <= 0) |
yu10078999 | 0:ea9c19e28331 | 25 | break; |
yu10078999 | 0:ea9c19e28331 | 26 | buffer[ret] = '\0'; |
yu10078999 | 0:ea9c19e28331 | 27 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
yu10078999 | 0:ea9c19e28331 | 28 | } |
yu10078999 | 0:ea9c19e28331 | 29 | |
yu10078999 | 0:ea9c19e28331 | 30 | sock.close(); |
yu10078999 | 0:ea9c19e28331 | 31 | |
yu10078999 | 0:ea9c19e28331 | 32 | eth.disconnect(); |
yu10078999 | 0:ea9c19e28331 | 33 | |
yu10078999 | 0:ea9c19e28331 | 34 | while(1) {} |
yu10078999 | 0:ea9c19e28331 | 35 | } |
yu10078999 | 0:ea9c19e28331 | 36 | |
yu10078999 | 0:ea9c19e28331 | 37 | // override the default weak function to provide a specific mac address |
yu10078999 | 0:ea9c19e28331 | 38 | extern "C" void mbed_mac_address(char *mac) |
yu10078999 | 0:ea9c19e28331 | 39 | { |
yu10078999 | 0:ea9c19e28331 | 40 | mac[0] = 0x01; |
yu10078999 | 0:ea9c19e28331 | 41 | mac[1] = 0x23; |
yu10078999 | 0:ea9c19e28331 | 42 | mac[2] = 0x45; |
yu10078999 | 0:ea9c19e28331 | 43 | mac[3] = 0x67; |
yu10078999 | 0:ea9c19e28331 | 44 | mac[4] = 0x89; |
yu10078999 | 0:ea9c19e28331 | 45 | mac[5] = 0xAB; |
yu10078999 | 0:ea9c19e28331 | 46 | }; |