![](/media/cache/profiles/af7fc8485f40dc495bc626d6dbe7faf1.50x50_q85.png)
TCP Echo Client example
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@3:3fbf0efec25a, 2012-08-01 (annotated)
- Committer:
- emilmont
- Date:
- Wed Aug 01 13:03:13 2012 +0000
- Revision:
- 3:3fbf0efec25a
- Parent:
- 2:563aa530f0dd
- Child:
- 6:dd8d1cf1cb9c
- Child:
- 9:a640b18e5931
Update socket library
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 | 3:3fbf0efec25a | 4 | const char* ECHO_SERVER_ADDRESS = "192.168.0.51"; |
emilmont | 3:3fbf0efec25a | 5 | const int ECHO_SERVER_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 | 3:3fbf0efec25a | 13 | TCPSocketConnection socket; |
emilmont | 3:3fbf0efec25a | 14 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
emilmont | 3:3fbf0efec25a | 15 | printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_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 | 3:3fbf0efec25a | 20 | socket.send_all(hello, sizeof(hello) - 1); |
emilmont | 1:a51d8ed156e6 | 21 | |
emilmont | 1:a51d8ed156e6 | 22 | char buf[256]; |
emilmont | 3:3fbf0efec25a | 23 | int n = socket.receive(buf, 256); |
emilmont | 2:563aa530f0dd | 24 | buf[n] = '\0'; |
emilmont | 1:a51d8ed156e6 | 25 | printf("%s", buf); |
emilmont | 1:a51d8ed156e6 | 26 | |
emilmont | 3:3fbf0efec25a | 27 | socket.close(); |
emilmont | 1:a51d8ed156e6 | 28 | eth.disconnect(); |
emilmont | 1:a51d8ed156e6 | 29 | |
emilmont | 1:a51d8ed156e6 | 30 | while(true) {} |
emilmont | 1:a51d8ed156e6 | 31 | } |