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-02
- Revision:
- 25:1a4f620b7af6
- Parent:
- 24:83a1d2bc8709
- Child:
- 26:897777a5ee04
File content as of revision 25:1a4f620b7af6:
/**
* @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.
*
*/
#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.
// Socket must create between "net.start" with "new Net()"
TcpSocket socket(512);
//Start network
net->start(cfg);
led1=1;
for(;;){
//connect to server
if(!socket.connect(IpAddr(192,168,128,195),1234)){
Thread::wait(1000);
}
//connected!
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);
}
led3=0;
}
led2=0;
led3=0;
led4=0;
socket.close(); //close the socket.
}
return 0;
}