![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Simple code for comunication via TCP between the mbed and PC.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
Fork of SimpleSocketExamples by
Diff: examples/echoclient.cpp
- Revision:
- 0:6dc3cfd058c6
diff -r 000000000000 -r 6dc3cfd058c6 examples/echoclient.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/echoclient.cpp Mon Feb 04 09:29:18 2013 +0000 @@ -0,0 +1,37 @@ +#include "SimpleSocket.h" + +void echoclient() { + printf("Echo server => "); + char server[32]; + scanf("%s", server); + + ClientSocket socket(server, 1234); + + if (socket) { + char message[80] = {}; + printf("Enter message => "); + int c = 0; + while (c < ' ' || 0x7E < c) + c = getc(stdin); + ungetc(c, stdin); + for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++) + message[i] = c; + + socket.printf("%s\r\n", message); + + // wait until data is received + while (!socket.available()) + ; + printf("Received: "); + + while (socket.available()) { + char buf[128]; + int len = socket.read(buf, sizeof(buf) - 1); + buf[len] = '\0'; + printf("%s", buf); + } + printf("\nClosing...\n"); + socket.close(); + } + printf("Done.\n"); +} \ No newline at end of file