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
Diff: main.cpp
- Revision:
- 26:897777a5ee04
- Parent:
- 25:1a4f620b7af6
- Child:
- 27:e06564324551
--- a/main.cpp Wed Oct 02 08:34:02 2013 +0000
+++ b/main.cpp Thu Oct 03 02:38:29 2013 +0000
@@ -1,8 +1,8 @@
/**
* @file
- * TCP client socket sample.<br/>
- * This program is to test of TCP client.
- * Connect to a TCP server, and send back the received data as is.
+ * 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"
@@ -32,51 +32,49 @@
cfg.setGateway(192,168,128,254);
- // Create tcp socket with 512 bytes RX buffer.
+ // 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()"
- TcpSocket socket(512);
+ // If you want to receive data without buffering, please email to MiMic project.
+ UdpSocket socket(1234,512);
//Start network
net->start(cfg);
- led1=1;
+ led1=1;
for(;;){
- //connect to server
- if(!socket.connect(IpAddr(192,168,128,195),1234)){
- Thread::wait(1000);
- }
- //connected!
+ //wait for packet
+ IpAddr peer_host;
+ unsigned short port;
+ const void* rx;
+ //get packet pointer and peer info.
led2=1;
- for(;;){
- led4=0;
- led3=1;
- //wait for data...
- const void* rx;
- //get read pointer
- int l=socket.precv(rx);
- if(l<0){
- break;
- }
- if(l==0){
- //timeout
- }else{
- //ok,echo back data.
- led4=1;
- //send data
- if(!socket.send(rx,l)){
- break;
- }
- //move read pointer.
- socket.pseek(l);
- }
+ 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;
- led4=0;
- socket.close(); //close the socket.
}
+ led1=0;
return 0;
}