MiMicSDK Tcp client socket sample program.

Dependencies:   NyFileSystems libMiMic mbed-rtos mbed

Fork of WebSocketSample by Ryo Iizuka

Committer:
nyatla
Date:
Thu Oct 03 05:00:02 2013 +0000
Revision:
26:ed7b937e664e
Parent:
25:1a4f620b7af6
Child:
29:e19c4305f24b
disable zeroconf services

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 4:0a280ed0a848 1 /**
nyatla 20:4b0b449ddb12 2 * @file
nyatla 25:1a4f620b7af6 3 * TCP client socket sample.<br/>
nyatla 25:1a4f620b7af6 4 * This program is to test of TCP client.
nyatla 25:1a4f620b7af6 5 * Connect to a TCP server, and send back the received data as is.
nyatla 20:4b0b449ddb12 6 *
nyatla 4:0a280ed0a848 7 */
nyatla 5:6a2a1644ea2c 8 #include "mbed.h"
nyatla 19:66d729b94d16 9 #include "rtos.h"
nyatla 5:6a2a1644ea2c 10 #include "SDFileSystem.h"
nyatla 0:ec1e45489427 11 #include "mimic.h"
nyatla 10:80c05810f911 12 #include "utils/PlatformInfo.h"
nyatla 19:66d729b94d16 13 #include "fsdata.h"
nyatla 19:66d729b94d16 14
nyatla 0:ec1e45489427 15
nyatla 23:6340bfc0bfe3 16 NetConfig cfg; //create network configulation
nyatla 19:66d729b94d16 17 Net* net;
nyatla 19:66d729b94d16 18
nyatla 25:1a4f620b7af6 19 DigitalOut led1(LED1);
nyatla 25:1a4f620b7af6 20 DigitalOut led2(LED2);
nyatla 25:1a4f620b7af6 21 DigitalOut led3(LED3);
nyatla 25:1a4f620b7af6 22 DigitalOut led4(LED4);
nyatla 0:ec1e45489427 23
nyatla 12:218b57d9a6d4 24
nyatla 0:ec1e45489427 25 int main()
nyatla 0:ec1e45489427 26 {
nyatla 19:66d729b94d16 27 net=new Net();//Net constructor must be created after started RTOS
nyatla 25:1a4f620b7af6 28
nyatla 23:6340bfc0bfe3 29 // manual setting
nyatla 23:6340bfc0bfe3 30 cfg.setIpAddr(192,168,128,39);
nyatla 23:6340bfc0bfe3 31 cfg.setNetMask(255,255,255,0);
nyatla 23:6340bfc0bfe3 32 cfg.setGateway(192,168,128,254);
nyatla 26:ed7b937e664e 33 cfg.setSrvUPnP(false);
nyatla 26:ed7b937e664e 34 cfg.setSrvMdns(false);
nyatla 25:1a4f620b7af6 35
nyatla 25:1a4f620b7af6 36 // Create tcp socket with 512 bytes RX buffer.
nyatla 25:1a4f620b7af6 37 // Socket must create between "net.start" with "new Net()"
nyatla 25:1a4f620b7af6 38 TcpSocket socket(512);
nyatla 25:1a4f620b7af6 39
nyatla 25:1a4f620b7af6 40 //Start network
nyatla 19:66d729b94d16 41 net->start(cfg);
nyatla 25:1a4f620b7af6 42
nyatla 25:1a4f620b7af6 43
nyatla 25:1a4f620b7af6 44 led1=1;
nyatla 25:1a4f620b7af6 45 for(;;){
nyatla 25:1a4f620b7af6 46 //connect to server
nyatla 25:1a4f620b7af6 47 if(!socket.connect(IpAddr(192,168,128,195),1234)){
nyatla 25:1a4f620b7af6 48 Thread::wait(1000);
nyatla 25:1a4f620b7af6 49 }
nyatla 25:1a4f620b7af6 50 //connected!
nyatla 25:1a4f620b7af6 51 led2=1;
nyatla 25:1a4f620b7af6 52 for(;;){
nyatla 25:1a4f620b7af6 53 led4=0;
nyatla 25:1a4f620b7af6 54 led3=1;
nyatla 25:1a4f620b7af6 55 //wait for data...
nyatla 25:1a4f620b7af6 56 const void* rx;
nyatla 25:1a4f620b7af6 57 //get read pointer
nyatla 25:1a4f620b7af6 58 int l=socket.precv(rx);
nyatla 25:1a4f620b7af6 59 if(l<0){
nyatla 25:1a4f620b7af6 60 break;
nyatla 25:1a4f620b7af6 61 }
nyatla 25:1a4f620b7af6 62 if(l==0){
nyatla 25:1a4f620b7af6 63 //timeout
nyatla 25:1a4f620b7af6 64 }else{
nyatla 25:1a4f620b7af6 65 //ok,echo back data.
nyatla 25:1a4f620b7af6 66 led4=1;
nyatla 25:1a4f620b7af6 67 //send data
nyatla 25:1a4f620b7af6 68 if(!socket.send(rx,l)){
nyatla 25:1a4f620b7af6 69 break;
nyatla 25:1a4f620b7af6 70 }
nyatla 25:1a4f620b7af6 71 //move read pointer.
nyatla 25:1a4f620b7af6 72 socket.pseek(l);
nyatla 25:1a4f620b7af6 73 }
nyatla 25:1a4f620b7af6 74 led3=0;
nyatla 25:1a4f620b7af6 75 }
nyatla 25:1a4f620b7af6 76 led2=0;
nyatla 25:1a4f620b7af6 77 led3=0;
nyatla 25:1a4f620b7af6 78 led4=0;
nyatla 25:1a4f620b7af6 79 socket.close(); //close the socket.
nyatla 25:1a4f620b7af6 80 }
nyatla 0:ec1e45489427 81 return 0;
nyatla 0:ec1e45489427 82 }
nyatla 25:1a4f620b7af6 83