MiMicSDK Tcp client socket sample program.
Dependencies: NyFileSystems libMiMic mbed-rtos mbed
Fork of WebSocketSample by
main.cpp@29:e19c4305f24b, 2015-09-15 (annotated)
- Committer:
- nyatla
- Date:
- Tue Sep 15 10:14:06 2015 +0000
- Revision:
- 29:e19c4305f24b
- Parent:
- 26:ed7b937e664e
update libMiMic
Who changed what in which revision?
User | Revision | Line number | New 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 | 29:e19c4305f24b | 27 | MiMicNetIf netif; |
nyatla | 29:e19c4305f24b | 28 | Net net(netif);//Net constructor must be created after started RTOS |
nyatla | 29:e19c4305f24b | 29 | NetConfig cfg; //create network configulation |
nyatla | 23:6340bfc0bfe3 | 30 | // manual setting |
nyatla | 23:6340bfc0bfe3 | 31 | cfg.setIpAddr(192,168,128,39); |
nyatla | 23:6340bfc0bfe3 | 32 | cfg.setNetMask(255,255,255,0); |
nyatla | 23:6340bfc0bfe3 | 33 | cfg.setGateway(192,168,128,254); |
nyatla | 26:ed7b937e664e | 34 | cfg.setSrvUPnP(false); |
nyatla | 26:ed7b937e664e | 35 | cfg.setSrvMdns(false); |
nyatla | 25:1a4f620b7af6 | 36 | |
nyatla | 25:1a4f620b7af6 | 37 | // Create tcp socket with 512 bytes RX buffer. |
nyatla | 25:1a4f620b7af6 | 38 | // Socket must create between "net.start" with "new Net()" |
nyatla | 29:e19c4305f24b | 39 | TcpSocket* socket=new TcpSocket(); |
nyatla | 25:1a4f620b7af6 | 40 | |
nyatla | 25:1a4f620b7af6 | 41 | //Start network |
nyatla | 29:e19c4305f24b | 42 | net.start(cfg); |
nyatla | 25:1a4f620b7af6 | 43 | |
nyatla | 25:1a4f620b7af6 | 44 | |
nyatla | 25:1a4f620b7af6 | 45 | led1=1; |
nyatla | 25:1a4f620b7af6 | 46 | for(;;){ |
nyatla | 25:1a4f620b7af6 | 47 | //connect to server |
nyatla | 29:e19c4305f24b | 48 | if(!socket->connect(IpAddr(192,168,128,195),1234)){ |
nyatla | 25:1a4f620b7af6 | 49 | Thread::wait(1000); |
nyatla | 25:1a4f620b7af6 | 50 | } |
nyatla | 25:1a4f620b7af6 | 51 | //connected! |
nyatla | 25:1a4f620b7af6 | 52 | led2=1; |
nyatla | 25:1a4f620b7af6 | 53 | for(;;){ |
nyatla | 25:1a4f620b7af6 | 54 | led4=0; |
nyatla | 25:1a4f620b7af6 | 55 | led3=1; |
nyatla | 25:1a4f620b7af6 | 56 | //wait for data... |
nyatla | 25:1a4f620b7af6 | 57 | const void* rx; |
nyatla | 25:1a4f620b7af6 | 58 | //get read pointer |
nyatla | 29:e19c4305f24b | 59 | int l=socket->precv(rx); |
nyatla | 25:1a4f620b7af6 | 60 | if(l<0){ |
nyatla | 25:1a4f620b7af6 | 61 | break; |
nyatla | 25:1a4f620b7af6 | 62 | } |
nyatla | 25:1a4f620b7af6 | 63 | if(l==0){ |
nyatla | 25:1a4f620b7af6 | 64 | //timeout |
nyatla | 25:1a4f620b7af6 | 65 | }else{ |
nyatla | 25:1a4f620b7af6 | 66 | //ok,echo back data. |
nyatla | 25:1a4f620b7af6 | 67 | led4=1; |
nyatla | 25:1a4f620b7af6 | 68 | //send data |
nyatla | 29:e19c4305f24b | 69 | if(!socket->send(rx,l)){ |
nyatla | 25:1a4f620b7af6 | 70 | break; |
nyatla | 25:1a4f620b7af6 | 71 | } |
nyatla | 25:1a4f620b7af6 | 72 | //move read pointer. |
nyatla | 29:e19c4305f24b | 73 | socket->pseek(l); |
nyatla | 25:1a4f620b7af6 | 74 | } |
nyatla | 25:1a4f620b7af6 | 75 | led3=0; |
nyatla | 25:1a4f620b7af6 | 76 | } |
nyatla | 25:1a4f620b7af6 | 77 | led2=0; |
nyatla | 25:1a4f620b7af6 | 78 | led3=0; |
nyatla | 25:1a4f620b7af6 | 79 | led4=0; |
nyatla | 29:e19c4305f24b | 80 | socket->close(); //close the socket. |
nyatla | 25:1a4f620b7af6 | 81 | } |
nyatla | 0:ec1e45489427 | 82 | return 0; |
nyatla | 0:ec1e45489427 | 83 | } |
nyatla | 25:1a4f620b7af6 | 84 |