.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPEchoClient by
main.cpp@1:a51d8ed156e6, 2012-07-26 (annotated)
- Committer:
- emilmont
- Date:
- Thu Jul 26 10:10:48 2012 +0000
- Revision:
- 1:a51d8ed156e6
- Parent:
- 0:e8f884c94549
- Child:
- 2:563aa530f0dd
First implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:e8f884c94549 | 1 | #include "mbed.h" |
emilmont | 1:a51d8ed156e6 | 2 | #include "EthernetInterface.h" |
emilmont | 1:a51d8ed156e6 | 3 | |
emilmont | 1:a51d8ed156e6 | 4 | const char* ECHO_SERVER_ADDRESS = "10.2.131.73"; |
emilmont | 1:a51d8ed156e6 | 5 | const int ECHO_PORT = 7; |
emilmont | 1:a51d8ed156e6 | 6 | |
emilmont | 1:a51d8ed156e6 | 7 | int main() { |
emilmont | 1:a51d8ed156e6 | 8 | EthernetInterface eth; |
emilmont | 1:a51d8ed156e6 | 9 | eth.init(); //Use DHCP |
emilmont | 1:a51d8ed156e6 | 10 | eth.connect(); |
emilmont | 1:a51d8ed156e6 | 11 | printf("IP Address is %s\n", eth.getIPAddress()); |
emilmont | 1:a51d8ed156e6 | 12 | |
emilmont | 1:a51d8ed156e6 | 13 | TCPSocketConnection sock; |
emilmont | 1:a51d8ed156e6 | 14 | while (sock.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) { |
emilmont | 1:a51d8ed156e6 | 15 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT); |
emilmont | 1:a51d8ed156e6 | 16 | wait(1); |
emilmont | 1:a51d8ed156e6 | 17 | } |
emilmont | 1:a51d8ed156e6 | 18 | |
emilmont | 1:a51d8ed156e6 | 19 | char hello[] = "Hello World\n"; |
emilmont | 1:a51d8ed156e6 | 20 | sock.send_all(hello, sizeof(hello) - 1); |
emilmont | 1:a51d8ed156e6 | 21 | |
emilmont | 1:a51d8ed156e6 | 22 | char buf[256]; |
emilmont | 1:a51d8ed156e6 | 23 | int n = sock.receive(buf, 256); |
emilmont | 1:a51d8ed156e6 | 24 | // buf[n] = '\0'; |
emilmont | 1:a51d8ed156e6 | 25 | printf("%s", buf); |
emilmont | 1:a51d8ed156e6 | 26 | |
emilmont | 1:a51d8ed156e6 | 27 | sock.close(); |
emilmont | 1:a51d8ed156e6 | 28 | eth.disconnect(); |
emilmont | 1:a51d8ed156e6 | 29 | |
emilmont | 1:a51d8ed156e6 | 30 | while(true) {} |
emilmont | 1:a51d8ed156e6 | 31 | } |