Dependencies: mbed EthernetNetIf
PMK_UDP_client.cpp
- Committer:
- nenad
- Date:
- 2015-04-30
- Revision:
- 1:42d45015ad30
- Parent:
- 0:527185ce17bc
File content as of revision 1:42d45015ad30:
#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(); } } }