Udp socket sample program. This program will send back the received packet.

Dependencies:   NyFileSystems libMiMic mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
nyatla
Date:
Thu Oct 03 02:38:29 2013 +0000
Parent:
25:1a4f620b7af6
Child:
27:e06564324551
Commit message:
udp echo back sample program.

Changed in this revision

libMiMic.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/libMiMic.lib	Wed Oct 02 08:34:02 2013 +0000
+++ b/libMiMic.lib	Thu Oct 03 02:38:29 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/nyatla/code/libMiMic/#cb5c3184c59f
+http://mbed.org/users/nyatla/code/libMiMic/#803de2088243
--- a/main.cpp	Wed Oct 02 08:34:02 2013 +0000
+++ b/main.cpp	Thu Oct 03 02:38:29 2013 +0000
@@ -1,8 +1,8 @@
 /**
  * @file
- * TCP client socket sample.<br/>
- * This program is to test of TCP client.
- * Connect to a TCP server, and send back the received data as is.
+ * Udpsocket sample.<br/>
+ * This program is to test of UDP socket.
+ * The program send a echo back packet when receive a packet.
  * 
  */
 #include "mbed.h"
@@ -32,51 +32,49 @@
     cfg.setGateway(192,168,128,254);    
 
 
-    // Create tcp socket with 512 bytes RX buffer.
+    // Create tcp socket with 512 bytes RX buffer.(This size same as maximum size of receiveable UDP packet)
     // Socket must create between "net.start" with "new Net()"
-    TcpSocket socket(512);
+    // If you want to receive data without buffering, please email to MiMic project.
+    UdpSocket socket(1234,512);
     
     //Start network
     net->start(cfg);
 
 
-    led1=1;    
+    led1=1;
     for(;;){
-        //connect to server
-        if(!socket.connect(IpAddr(192,168,128,195),1234)){
-            Thread::wait(1000);
-        }
-        //connected!
+        //wait for packet
+        IpAddr peer_host;
+        unsigned short port;
+        const void* rx;
+        //get packet pointer and peer info.
         led2=1;
-        for(;;){
-            led4=0;
-            led3=1;
-            //wait for data...
-            const void* rx;
-            //get read pointer
-            int l=socket.precv(rx);
-            if(l<0){
-                break;
-            }
-            if(l==0){
-                //timeout
-            }else{
-                //ok,echo back data.
-                led4=1;
-                //send data
-                if(!socket.send(rx,l)){
-                    break;
-                }
-                //move read pointer.
-                socket.pseek(l);
-            }
+        int l=socket.precvfrom(rx,&peer_host,&port);
+        if(l<0){
+            //Error
+            led2=0;
+            break;
+        }
+        if(l==0){
+            //timeout
+            led2=0;
+            continue;
+        }
+        led3=1;
+        if(!socket.sendTo(peer_host,port,rx,l)){
+            //Error
+            led2=0;
             led3=0;
+            led4=1;
+            break;
         }
+        Thread::wait(100);//show LED blinking!
+        //move to next packet
+        socket.precvnext();
         led2=0;
         led3=0;
-        led4=0;
-        socket.close(); //close the socket.
     }
+    led1=0;
     return 0;
 }