TCP Socket Hello World with Ethernet
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPSocket_HelloWorld by
main.cpp@2:e087e9b789e9, 2012-07-02 (annotated)
- Committer:
- emilmont
- Date:
- Mon Jul 02 10:54:00 2012 +0000
- Revision:
- 2:e087e9b789e9
- Parent:
- 1:e2652bd064c6
- Child:
- 5:01f6c3e112af
Remove debug directives avoiding to change the default settings of the serial port. Add print of the IP address assigned by DHCP.
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 | |
donatien | 0:bb128f0e952f | 4 | int main() |
donatien | 0:bb128f0e952f | 5 | { |
donatien | 0:bb128f0e952f | 6 | EthernetInterface eth; |
donatien | 0:bb128f0e952f | 7 | eth.init(); //Use DHCP |
donatien | 0:bb128f0e952f | 8 | eth.connect(); |
emilmont | 2:e087e9b789e9 | 9 | printf("IP Address is %s\n", eth.getIPAddress()); |
donatien | 0:bb128f0e952f | 10 | |
donatien | 0:bb128f0e952f | 11 | TCPSocket sock; |
donatien | 0:bb128f0e952f | 12 | sock.connect("mbed.org", 80); |
donatien | 0:bb128f0e952f | 13 | |
donatien | 1:e2652bd064c6 | 14 | char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n"; |
donatien | 1:e2652bd064c6 | 15 | sock.send(http_cmd, sizeof(http_cmd) - 1, 3000); |
donatien | 0:bb128f0e952f | 16 | |
donatien | 0:bb128f0e952f | 17 | char in_buf[256]; |
donatien | 0:bb128f0e952f | 18 | bool firstIteration = true; |
donatien | 0:bb128f0e952f | 19 | int ret; |
donatien | 0:bb128f0e952f | 20 | do |
donatien | 0:bb128f0e952f | 21 | { |
donatien | 1:e2652bd064c6 | 22 | ret = sock.receive(in_buf, 255, firstIteration?3000:0); |
donatien | 0:bb128f0e952f | 23 | in_buf[ret] = '\0'; |
donatien | 0:bb128f0e952f | 24 | |
donatien | 0:bb128f0e952f | 25 | printf("Received %d chars from server: %s\n", ret, in_buf); |
donatien | 0:bb128f0e952f | 26 | firstIteration = false; |
donatien | 0:bb128f0e952f | 27 | } while( ret > 0 ); |
donatien | 0:bb128f0e952f | 28 | |
donatien | 0:bb128f0e952f | 29 | sock.close(); |
donatien | 0:bb128f0e952f | 30 | |
donatien | 0:bb128f0e952f | 31 | eth.disconnect(); |
donatien | 0:bb128f0e952f | 32 | |
donatien | 0:bb128f0e952f | 33 | while(1) { |
donatien | 0:bb128f0e952f | 34 | } |
donatien | 0:bb128f0e952f | 35 | } |