Dependencies: mbed EthernetNetIf
Diff: PMK_UDP_client.cpp
- Revision:
- 0:527185ce17bc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PMK_UDP_client.cpp Tue May 03 22:13:06 2011 +0000 @@ -0,0 +1,66 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "UDPSocket.h" + +//EthernetNetIf eth ; +EthernetNetIf eth( + IpAddr(192,168,1,111), //IP Address + IpAddr(255,255,255,0), //Network Mask + IpAddr(192,168,0,1), //Gateway + IpAddr(192,168,0,1) //DNS +); + +UDPSocket udp; +int global_counter=0; +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("Received from %d.%d.%d.%d: %s\r\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf); + printf("\n"); + } + break; + } +} + +int main() { + printf("Setting up...\n"); + EthernetErr ethErr = eth.setup(30000); + if(ethErr) + { + printf("Error %d in setup.\n", ethErr); + return -1; + } + printf("Setup OK\n"); + + Host multicast(IpAddr(224,0,0,0), 50000); //Join multicast group on port 50000 + + udp.setOnEvent(&onUDPSocketEvent); + + udp.bind(multicast); + + Timer tmr; + tmr.start(); + while(true) + { + Net::poll(); + if(tmr.read() > 2) + { + char Buffer[100]={0}; + global_counter++; + sprintf(Buffer,"Counter=%d", global_counter); + printf("Sent to group: Counter=%d\r\n", global_counter); + udp.sendto( Buffer, strlen(Buffer)+1, &multicast ); + tmr.reset(); + } + } + + +}