Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NyFileSystems libMiMic mbed-rtos mbed
main.cpp
- Committer:
- nyatla
- Date:
- 2013-10-03
- Revision:
- 26:897777a5ee04
- Parent:
- 25:1a4f620b7af6
- Child:
- 27:e06564324551
File content as of revision 26:897777a5ee04:
/**
* @file
* Udpsocket sample.<br/>
* This program is to test of UDP socket.
* The program send a echo back packet when receive a packet.
*
*/
#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "mimic.h"
#include "utils/PlatformInfo.h"
#include "fsdata.h"
NetConfig cfg; //create network configulation
Net* net;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
int main()
{
net=new Net();//Net constructor must be created after started RTOS
// manual setting
cfg.setIpAddr(192,168,128,39);
cfg.setNetMask(255,255,255,0);
cfg.setGateway(192,168,128,254);
// Create tcp socket with 512 bytes RX buffer.(This size same as maximum size of receiveable UDP packet)
// Socket must create between "net.start" with "new Net()"
// If you want to receive data without buffering, please email to MiMic project.
UdpSocket socket(1234,512);
//Start network
net->start(cfg);
led1=1;
for(;;){
//wait for packet
IpAddr peer_host;
unsigned short port;
const void* rx;
//get packet pointer and peer info.
led2=1;
int l=socket.precvfrom(rx,&peer_host,&port);
if(l<0){
//Error
led2=0;
break;
}
if(l==0){
//timeout
led2=0;
continue;
}
led3=1;
if(!socket.sendTo(peer_host,port,rx,l)){
//Error
led2=0;
led3=0;
led4=1;
break;
}
Thread::wait(100);//show LED blinking!
//move to next packet
socket.precvnext();
led2=0;
led3=0;
}
led1=0;
return 0;
}