Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
Diff: Example/echoclient.cpp
- Revision:
- 15:ae9aff693b07
- Parent:
- 14:c6f5c0833cee
- Child:
- 24:9c7a2e830ef6
--- a/Example/echoclient.cpp Fri Aug 19 02:42:57 2011 +0000
+++ b/Example/echoclient.cpp Fri Aug 19 05:18:29 2011 +0000
@@ -1,33 +1,27 @@
-#include "mbed.h"
-#include "SimpleSocket.h"
-
-void echoclient(char *server, int port, char *message) {
-
- ClientSocket client(server, port);
-// wait(1);
-// Net::poll();
-//TCPSOCKET_CONNECTED: Connected to host.
- if (client.connected()) {
- // wait(1);
- // Net::poll();
- // wait(1);
- // Net::poll();
- printf("*** send: %s", message);
- client.writef(message);
- printf("*** sent: %s", message);
- //TCPSOCKET_WRITEABLE: Can write data to buf.
- //TCPSOCKET_READABLE: Data in buf.
-
- int total = 0;
- while (client.connected()) {
- if (client.available()) {
- char buf[129];
- int len = client.read(buf, sizeof(buf) - 1);
- buf[len] = '\0';
- printf(buf);
- total += len;
- }
- }
- printf("\n\n*** Total %d ***\n", total);
- }
+#include "mbed.h"
+#include "SimpleSocket.h"
+
+void echoclient(char *server, int port, char *message) {
+ printf("** echoclient, server = %s, port = %d, message = %s\n", server, port, message);
+
+ ClientSocket socket(server, port);
+
+ if (socket.connected()) {
+ socket.writef(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(buf);
+ }
+ printf("Closing...\n");
+ socket.close();
+ }
+ printf("Done.\n");
}
\ No newline at end of file