Threads + Network Demo

Dependencies:   EthernetInterface mbed-rtos mbed-src

Fork of TestNetComm by Malcolm Nixon

Revision:
4:f2789e665f4e
Parent:
3:1e86c29d1eac
Child:
5:18415c556f04
--- a/main.cpp	Sun May 04 03:05:22 2014 +0000
+++ b/main.cpp	Sun May 04 04:31:58 2014 +0000
@@ -2,60 +2,58 @@
 #include "Thread.h"
 #include "mbed.h"
 
+// Configure the LEDs (off)
+DigitalOut led1(LED1, 1);
 DigitalOut led2(LED2, 1);
     
-// Blink function
-void blink(const void *param)
+
+void ledThreadProc(const void *param)
 {
-    led2 = !led2;
+    // Toggle the Green LED
+    for (;;)
+    {
+        led2 = !led2;
+        Thread::wait(1000);
+    }
 }
 
 int main(void)
 {
-    // Create a digital output on LED1
-    DigitalOut led1(LED1, 1);
+    Thread ledThread(ledThreadProc);
 
-    // Start the blink timer at 1 second interval
-    RtosTimer timer(blink);
-    timer.start(1000);
-    
-    // Construct the Ethernet interface
+    // Start the network
     EthernetInterface eth;
     eth.init("192.168.5.100", "255.255.255.0", "0.0.0.0");
     eth.connect();
-    
+
     // Construct the server
+    TCPSocketConnection conn;
     TCPSocketServer svr;
     svr.bind(1234);
     svr.listen();
+
     for (;;)
     {
         // Accept the next connection
-        TCPSocketConnection conn;
-        if (svr.accept(conn) == 0)
+        svr.accept(conn);
+            
+        // Manage the connection
+        for (;;)
         {
-            // Turn the TCP_NODELAY option on
-            int opt = 1;
-            conn.set_option(IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
-            
-            // Manage the connection
-            for (;;)
+            // Read the input
+            char buf[32];
+            int nr = conn.receive(buf, 32);
+            if (nr <= 0)
             {
-                // Read the input
-                char buf[128];
-                int nr = conn.receive(buf, 128);
-                if (nr <= 0)
-                {
-                    break;
-                }
-                
-                // Toggle the output on led1
-                led1 = !led1;
-                
-                // Write the response twice
-                conn.send(buf, nr);
-                conn.send(buf, nr);
+                break;
             }
+            
+            // Toggle the output on led1
+            led1 = !led1;
+            
+            // Write the response twice
+            conn.send(buf, nr);
+            conn.send(buf, nr);
         }
-    }    
-}
\ No newline at end of file
+    }
+}