Hi,
I am relatively new to embedded ethernet. I have gone through the LWIP documentation and read about UDP and TCP basics from Jan Axelson's Ethernet Complete book. I still would like to know, how the UDP is implemented in the UDP socket example. I could understand setting up the Ethernet Interface of Mbed with EthernetNetif. What is the function of HOST , and how does the UDPsocket event function work. I read about multicast, and broadcast conn, however, it would be nice, if someone could explain it with reference to the below program.
00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "UDPSocket.h"
00004 EthernetNetIf eth;
00005 UDPSocket cfgSocket;
00006
00007 void onUDPSocketEvent(UDPSocketEvent e) {
00008 if ( e == UDPSOCKET_READABLE ) {
00009 char buf[64] = {0};
00010 Host host;
00011 while ( int len = cfgSocket.recvfrom( buf, 63, &host ) ) {
00012 if ( len <= 0 )
00013 break;
00014 printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf);
00015 }
00016 }
00017 }
00018
00019 int main() {
00020 // pc.baud(115200);
00021 printf ("HEllo World - 7 *\n");
00022
00023 EthernetErr ethErr = eth.setup();
00024 if ( ethErr == ETH_OK ) {
00025 IpAddr ip = eth.getIp();
00026 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
00027 } else printf ("ETHERNETSETUP FAILED\n");
00028
00029 printf("seting event\n");
00030 cfgSocket.setOnEvent(&onUDPSocketEvent);
00031 printf("seting host\n");
00032 Host x(IpAddr(),5555);
00033 printf("seting bind\n");
00034
00035 UDPSocketErr udpErr = cfgSocket.bind( x );
00036 if ( udpErr != UDPSOCKET_OK )
00037 printf("error %d\n", udpErr);
00038
00039 int i = 0;
00040 Host brdHost(IpAddr(255,255,255,255), 5555); //broadcast test msg
00041 char cfgRqstMsg[]="SERVER CONFIG REQUEST";
00042
00043 Timer tmr;
00044 tmr.start();
00045 while (1) {
00046 Net::poll();
00047 if ( tmr.read() > 5) {
00048 tmr.reset();
00049 int nSent = cfgSocket.sendto(cfgRqstMsg,strlen(cfgRqstMsg),&brdHost);
00050 printf("PLONKER %d :: %d\n",++i,nSent);
00051 if ( nSent < 0 )
00052 printf("error %d\n", (UDPSocketErr)nSent);
00053 }
00054 }
00055 }
Hi,
I am relatively new to embedded ethernet. I have gone through the LWIP documentation and read about UDP and TCP basics from Jan Axelson's Ethernet Complete book. I still would like to know, how the UDP is implemented in the UDP socket example. I could understand setting up the Ethernet Interface of Mbed with EthernetNetif. What is the function of HOST , and how does the UDPsocket event function work. I read about multicast, and broadcast conn, however, it would be nice, if someone could explain it with reference to the below program.
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "UDPSocket.h" 00004 EthernetNetIf eth; 00005 UDPSocket cfgSocket; 00006 00007 void onUDPSocketEvent(UDPSocketEvent e) { 00008 if ( e == UDPSOCKET_READABLE ) { 00009 char buf[64] = {0}; 00010 Host host; 00011 while ( int len = cfgSocket.recvfrom( buf, 63, &host ) ) { 00012 if ( len <= 0 ) 00013 break; 00014 printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf); 00015 } 00016 } 00017 } 00018 00019 int main() { 00020 // pc.baud(115200); 00021 printf ("HEllo World - 7 *\n"); 00022 00023 EthernetErr ethErr = eth.setup(); 00024 if ( ethErr == ETH_OK ) { 00025 IpAddr ip = eth.getIp(); 00026 printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); 00027 } else printf ("ETHERNETSETUP FAILED\n"); 00028 00029 printf("seting event\n"); 00030 cfgSocket.setOnEvent(&onUDPSocketEvent); 00031 printf("seting host\n"); 00032 Host x(IpAddr(),5555); 00033 printf("seting bind\n"); 00034 00035 UDPSocketErr udpErr = cfgSocket.bind( x ); 00036 if ( udpErr != UDPSOCKET_OK ) 00037 printf("error %d\n", udpErr); 00038 00039 int i = 0; 00040 Host brdHost(IpAddr(255,255,255,255), 5555); //broadcast test msg 00041 char cfgRqstMsg[]="SERVER CONFIG REQUEST"; 00042 00043 Timer tmr; 00044 tmr.start(); 00045 while (1) { 00046 Net::poll(); 00047 if ( tmr.read() > 5) { 00048 tmr.reset(); 00049 int nSent = cfgSocket.sendto(cfgRqstMsg,strlen(cfgRqstMsg),&brdHost); 00050 printf("PLONKER %d :: %d\n",++i,nSent); 00051 if ( nSent < 0 ) 00052 printf("error %d\n", (UDPSocketErr)nSent); 00053 } 00054 } 00055 }