![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Based on Donatien Garnier UDPexample program
Dependencies: mbed EthernetNetIf
PMK_UDP_server.cpp@0:946ce6cfc0e9, 2011-05-03 (annotated)
- Committer:
- nenad
- Date:
- Tue May 03 22:13:15 2011 +0000
- Revision:
- 0:946ce6cfc0e9
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nenad | 0:946ce6cfc0e9 | 1 | #include "mbed.h" |
nenad | 0:946ce6cfc0e9 | 2 | #include "EthernetNetIf.h" |
nenad | 0:946ce6cfc0e9 | 3 | #include "UDPSocket.h" |
nenad | 0:946ce6cfc0e9 | 4 | |
nenad | 0:946ce6cfc0e9 | 5 | DigitalOut led4(LED4, "led4"); |
nenad | 0:946ce6cfc0e9 | 6 | |
nenad | 0:946ce6cfc0e9 | 7 | //EthernetNetIf eth ; |
nenad | 0:946ce6cfc0e9 | 8 | EthernetNetIf eth( |
nenad | 0:946ce6cfc0e9 | 9 | IpAddr(192,168,1,110), //IP Address |
nenad | 0:946ce6cfc0e9 | 10 | IpAddr(255,255,255,0), //Network Mask |
nenad | 0:946ce6cfc0e9 | 11 | IpAddr(192,168,0,1), //Gateway |
nenad | 0:946ce6cfc0e9 | 12 | IpAddr(192,168,0,1) //DNS |
nenad | 0:946ce6cfc0e9 | 13 | ); |
nenad | 0:946ce6cfc0e9 | 14 | |
nenad | 0:946ce6cfc0e9 | 15 | UDPSocket udp; |
nenad | 0:946ce6cfc0e9 | 16 | int global_counter=0; |
nenad | 0:946ce6cfc0e9 | 17 | void onUDPSocketEvent(UDPSocketEvent e) |
nenad | 0:946ce6cfc0e9 | 18 | { |
nenad | 0:946ce6cfc0e9 | 19 | switch(e) |
nenad | 0:946ce6cfc0e9 | 20 | { |
nenad | 0:946ce6cfc0e9 | 21 | case UDPSOCKET_READABLE: //The only event for now |
nenad | 0:946ce6cfc0e9 | 22 | char buf[64] = {0}; |
nenad | 0:946ce6cfc0e9 | 23 | Host host; |
nenad | 0:946ce6cfc0e9 | 24 | while( int len = udp.recvfrom( buf, 63, &host ) ) |
nenad | 0:946ce6cfc0e9 | 25 | { |
nenad | 0:946ce6cfc0e9 | 26 | if( len <= 0 ) |
nenad | 0:946ce6cfc0e9 | 27 | break; |
nenad | 0:946ce6cfc0e9 | 28 | printf("Received from %d.%d.%d.%d: %s and sent back to goup\r\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf); |
nenad | 0:946ce6cfc0e9 | 29 | } |
nenad | 0:946ce6cfc0e9 | 30 | udp.sendto( buf,strlen(buf)+1, &host ); |
nenad | 0:946ce6cfc0e9 | 31 | break; |
nenad | 0:946ce6cfc0e9 | 32 | } |
nenad | 0:946ce6cfc0e9 | 33 | } |
nenad | 0:946ce6cfc0e9 | 34 | |
nenad | 0:946ce6cfc0e9 | 35 | int main() { |
nenad | 0:946ce6cfc0e9 | 36 | printf("Setting up...\n"); |
nenad | 0:946ce6cfc0e9 | 37 | EthernetErr ethErr = eth.setup(30000); |
nenad | 0:946ce6cfc0e9 | 38 | if(ethErr) |
nenad | 0:946ce6cfc0e9 | 39 | { |
nenad | 0:946ce6cfc0e9 | 40 | printf("Error %d in setup.\n", ethErr); |
nenad | 0:946ce6cfc0e9 | 41 | return -1; |
nenad | 0:946ce6cfc0e9 | 42 | } |
nenad | 0:946ce6cfc0e9 | 43 | printf("Setup OK\n"); |
nenad | 0:946ce6cfc0e9 | 44 | |
nenad | 0:946ce6cfc0e9 | 45 | Host multicast(IpAddr(224,0,0,0), 50000); //Join multicast group on port 50000 |
nenad | 0:946ce6cfc0e9 | 46 | |
nenad | 0:946ce6cfc0e9 | 47 | udp.setOnEvent(&onUDPSocketEvent); |
nenad | 0:946ce6cfc0e9 | 48 | udp.bind(multicast); |
nenad | 0:946ce6cfc0e9 | 49 | |
nenad | 0:946ce6cfc0e9 | 50 | Timer tmr; |
nenad | 0:946ce6cfc0e9 | 51 | tmr.start(); |
nenad | 0:946ce6cfc0e9 | 52 | while(true) |
nenad | 0:946ce6cfc0e9 | 53 | { |
nenad | 0:946ce6cfc0e9 | 54 | Net::poll(); |
nenad | 0:946ce6cfc0e9 | 55 | if(tmr.read() > 0.5) |
nenad | 0:946ce6cfc0e9 | 56 | { |
nenad | 0:946ce6cfc0e9 | 57 | led4=!led4; //Show that we are alive |
nenad | 0:946ce6cfc0e9 | 58 | tmr.reset(); |
nenad | 0:946ce6cfc0e9 | 59 | } |
nenad | 0:946ce6cfc0e9 | 60 | } |
nenad | 0:946ce6cfc0e9 | 61 | } |