UDP server example project for the NetworkAPI library

Dependencies:   EthernetInterface NetworkAPI mbed-rtos mbed

Fork of TCP_Client_Example by Roy van Dam

Files at this revision

API Documentation at this revision

Comitter:
NegativeBlack
Date:
Wed Jul 18 13:22:09 2012 +0000
Parent:
8:d55cac25e637
Child:
10:b260ea0eb330
Commit message:
Implemented TCP socket 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	Wed Jul 18 11:24:12 2012 +0000
+++ b/NetworkAPI.lib	Wed Jul 18 13:22:09 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/NegativeBlack/code/NetworkAPI/#d30db8752485
+http://mbed.org/users/NegativeBlack/code/NetworkAPI/#d854fa394f85
--- a/main.cpp	Wed Jul 18 11:24:12 2012 +0000
+++ b/main.cpp	Wed Jul 18 13:22:09 2012 +0000
@@ -1,8 +1,9 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
+#include "NetworkAPI/buffer.hpp"
 #include "NetworkAPI/ip/address.hpp"
-#include "NetworkAPI/udp/socket.hpp"
+#include "NetworkAPI/tcp/socket.hpp"
 
 int
 main()
@@ -12,42 +13,33 @@
     interface.connect();
     printf("IP Address is %s\n\r", interface.getIPAddress());
   
-    network::udp::Socket socket;
-    char buffer[1024];
-    int result = 0;
+    int result;
+  
+    network::tcp::Socket socket;
+    network::Buffer buffer(256);
+    std::string request("GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n");
     
     if (socket.open() < 0) {
-        printf("Failed to open UDP Socket\n\r");
+        printf("Failed to open TCP Socket\n\r");
         return -1;
     }
     
-    if (socket.bind(42) < 0) {
-        printf("Failed to bind UDP Socket to port 42\n\r");
+    if (socket.connect("mbed.org", 80) < 0) {
+        printf("Failed to connect with mbed.org\n\r");
+        return -1;
+    }
+    
+    if (socket.write((void *)request.data(), request.size()) < 0) {
+        printf("Failed to write HTTP request\n\r");
+        return -1;
     }
     
-    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;
-       }
-    }
-        
+    do
+    {
+        result = socket.read(buffer);   
+        printf("Received %d bytes:\n\r%s\n\r", result, (char *)buffer.pointer());
+    } while(result > 0);
+    
+    socket.close();
     return 0;
 }
\ No newline at end of file