TCPSocket_Ethernet modify
Dependencies: EthernetInterface2 mbed-rtos mbed
Fork of TCPSocket_HelloWorld by
main.cpp@15:d7e821422e97, 2017-05-30 (annotated)
- Committer:
- artpes
- Date:
- Tue May 30 16:07:41 2017 +0000
- Revision:
- 15:d7e821422e97
- Parent:
- 11:59dcefdda506
Tcp socket ethernet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
donatien | 0:bb128f0e952f | 2 | #include "EthernetInterface.h" |
donatien | 0:bb128f0e952f | 3 | |
artpes | 15:d7e821422e97 | 4 | Serial pc(USBTX, USBRX); |
artpes | 15:d7e821422e97 | 5 | |
emilmont | 7:65188f4a8c25 | 6 | int main() { |
donatien | 0:bb128f0e952f | 7 | EthernetInterface eth; |
donatien | 0:bb128f0e952f | 8 | eth.init(); //Use DHCP |
artpes | 15:d7e821422e97 | 9 | int i = eth.connect(); |
artpes | 15:d7e821422e97 | 10 | pc.printf("Connection = %d, ",i); |
artpes | 15:d7e821422e97 | 11 | pc.printf("\nIP Address is %s -\nGateway: %s -\nMACAddress %s \n", eth.getIPAddress(),eth.getGateway(),eth.getMACAddress()); |
donatien | 0:bb128f0e952f | 12 | |
emilmont | 7:65188f4a8c25 | 13 | TCPSocketConnection sock; |
artpes | 15:d7e821422e97 | 14 | sock.connect("www.unife.it", 80); |
donatien | 0:bb128f0e952f | 15 | |
artpes | 15:d7e821422e97 | 16 | char http_cmd[] = "GET /ing/lm.infoauto HTTP/1.0\n\n"; |
emilmont | 11:59dcefdda506 | 17 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
emilmont | 7:65188f4a8c25 | 18 | |
emilmont | 9:4757a976148d | 19 | char buffer[300]; |
donatien | 0:bb128f0e952f | 20 | int ret; |
emilmont | 7:65188f4a8c25 | 21 | while (true) { |
emilmont | 9:4757a976148d | 22 | ret = sock.receive(buffer, sizeof(buffer)-1); |
emilmont | 7:65188f4a8c25 | 23 | if (ret <= 0) |
emilmont | 7:65188f4a8c25 | 24 | break; |
emilmont | 9:4757a976148d | 25 | buffer[ret] = '\0'; |
artpes | 15:d7e821422e97 | 26 | pc.printf("Received %d chars from server:\n%s\n", ret, buffer); |
emilmont | 7:65188f4a8c25 | 27 | } |
donatien | 0:bb128f0e952f | 28 | |
emilmont | 7:65188f4a8c25 | 29 | sock.close(); |
donatien | 0:bb128f0e952f | 30 | |
emilmont | 7:65188f4a8c25 | 31 | eth.disconnect(); |
donatien | 5:01f6c3e112af | 32 | |
emilmont | 9:4757a976148d | 33 | while(1) {} |
donatien | 0:bb128f0e952f | 34 | } |