UDP Sockets use example

Dependencies:   EthernetNetIf mbed

Revision:
0:5874b7a688d1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UDPSocketExample.cpp	Fri Aug 06 11:19:16 2010 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "UDPSocket.h"
+
+EthernetNetIf eth;
+UDPSocket udp;
+
+void onUDPSocketEvent(UDPSocketEvent e)
+{
+  switch(e)
+  {
+  case UDPSOCKET_READABLE: //The only event for now
+    char buf[64] = {0};
+    Host host;
+    while( int len = udp.recvfrom( buf, 63, &host ) )
+    {
+      if( len <= 0 )
+        break;
+      printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf);
+    }
+    break;
+  }
+}
+
+int main() {
+  printf("Setting up...\n");
+  EthernetErr ethErr = eth.setup();
+  if(ethErr)
+  {
+    printf("Error %d in setup.\n", ethErr);
+    return -1;
+  }
+  printf("Setup OK\n");
+  
+  Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); //Join multicast group on port 50000
+ 
+  udp.setOnEvent(&onUDPSocketEvent);
+  
+  udp.bind(multicast);
+  
+  Timer tmr;
+  tmr.start();
+  while(true)
+  {
+    Net::poll();
+    if(tmr.read() > 5)
+    {
+      tmr.reset();
+      const char* str = "Hello world!";
+      udp.sendto( str, strlen(str), &multicast );
+      printf("%s\n", str);
+    }
+  }
+
+  
+}