Example application that implements a simple TCP server on top of the NetworkAPI

Dependencies:   mbed mbed-rtos EthernetInterface NetworkAPI

Files at this revision

API Documentation at this revision

Comitter:
NegativeBlack
Date:
Wed Jul 18 11:24:12 2012 +0000
Parent:
7:c4ea5b5a7218
Child:
9:a4c85bea2d77
Commit message:
Added simple UDP communication example.

Changed in this revision

NetworkAPI.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/NetworkAPI.lib	Tue Jul 17 18:32:46 2012 +0000
+++ b/NetworkAPI.lib	Wed Jul 18 11:24:12 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/NegativeBlack/code/NetworkAPI/#e283a0062097
+http://mbed.org/users/NegativeBlack/code/NetworkAPI/#d30db8752485
--- a/main.cpp	Tue Jul 17 18:32:46 2012 +0000
+++ b/main.cpp	Wed Jul 18 11:24:12 2012 +0000
@@ -2,6 +2,7 @@
 #include "EthernetInterface.h"
 
 #include "NetworkAPI/ip/address.hpp"
+#include "NetworkAPI/udp/socket.hpp"
 
 int
 main()
@@ -10,10 +11,43 @@
     interface.init();
     interface.connect();
     printf("IP Address is %s\n\r", interface.getIPAddress());
-
-    network::ip::Address address;
-    address.fromHostname("www.google.nl");
+  
+    network::udp::Socket socket;
+    char buffer[1024];
+    int result = 0;
+    
+    if (socket.open() < 0) {
+        printf("Failed to open UDP Socket\n\r");
+        return -1;
+    }
+    
+    if (socket.bind(42) < 0) {
+        printf("Failed to bind UDP Socket to port 42\n\r");
+    }
     
-    printf("Network address %s\n", address.toString().c_str());
+    while (true) {
+        result = socket.receive(buffer, 1024);
+        
+        switch (result) {
+            case -1:
+                printf("Failed to read from UDP Socket\n\r");
+                return -1;
+            
+            case 0:
+                printf("Nothing received...?\n\r");
+                continue;
+            
+            default:
+                printf("Received %d bytes from %s:%d\n\r", result,
+                    socket.getRemoteEndpoint().getAddress().toString().c_str(),
+                    socket.getRemoteEndpoint().getPort());
+                
+                if (!socket.getRemoteEndpoint().getAddress().isEmpty()) {
+                    socket.send(buffer, result, socket.getRemoteEndpoint());
+                }
+                continue;
+       }
+    }
+        
     return 0;
 }
\ No newline at end of file