Demonstrate unexpected interaction between send() and recv()

Files at this revision

API Documentation at this revision

Comitter:
infinnovation
Date:
Thu Aug 18 16:17:35 2016 +0000
Commit message:
Initial commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b380efeccf3a main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Aug 18 16:17:35 2016 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+Serial pc(USBTX, USBRX);
+EthernetInterface eth;
+
+void
+s2c_run(TCPSocket *sock)
+{
+    int ret;
+    char c;
+    while (true) {
+        ret = sock->recv(&c, 1);
+        if (ret == 1) {
+            printf("recv %02x=%c\r\n", c, (0x20 <= c && c < 0x7f) ? c : '?');
+        } else if (ret == 0) {
+            printf("EOS\r\n");
+        } else {
+            error("recv: %d\r\n", ret);
+        }
+    }
+}
+
+int main(void)
+{
+    Thread s2c;
+    int err;
+
+    printf("\r\n== sendrecv ==\r\n");
+    err = eth.connect();
+    if (err) error("connect: %d\n", err);
+    printf("ip %s\r\n", eth.get_ip_address());
+    TCPServer srv;
+    err = srv.open(&eth);
+    if (err) error("open: %d\r\n", err);
+    err = srv.bind(8084);
+    if (err) error("bind: %d\r\n", err);
+    err = srv.listen(1);
+    if (err) error("listen: %d\r\n", err);
+    TCPSocket sock;
+    err = srv.accept(&sock);
+    if (err) error("accept: %d\r\n", err);
+    printf("accepted connection\r\n");
+    s2c.start(Callback<void()>(&sock, s2c_run));
+    while (true) {
+        printf("tick...\r\n");
+        err = sock.send("tick\n", 5);
+        if (err <= 0) error("send: %d\r\n", err);
+        printf("...sent\r\n");
+        wait(1.0);
+    }
+}
diff -r 000000000000 -r b380efeccf3a mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Aug 18 16:17:35 2016 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#0993ae540c5c4b5ce3004bead1aefc0c6d1b3ea0