Udp socket sample program. This program will send back the received packet.
Dependencies: NyFileSystems libMiMic mbed-rtos mbed
main.cpp@25:1a4f620b7af6, 2013-10-02 (annotated)
- Committer:
- nyatla
- Date:
- Wed Oct 02 08:34:02 2013 +0000
- Revision:
- 25:1a4f620b7af6
- Parent:
- 24:83a1d2bc8709
- Child:
- 26:897777a5ee04
Tcp client sample
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 | 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 | 12:218b57d9a6d4 | 33 | |
nyatla | 25:1a4f620b7af6 | 34 | |
nyatla | 25:1a4f620b7af6 | 35 | // Create tcp socket with 512 bytes RX buffer. |
nyatla | 25:1a4f620b7af6 | 36 | // Socket must create between "net.start" with "new Net()" |
nyatla | 25:1a4f620b7af6 | 37 | TcpSocket socket(512); |
nyatla | 25:1a4f620b7af6 | 38 | |
nyatla | 25:1a4f620b7af6 | 39 | //Start network |
nyatla | 19:66d729b94d16 | 40 | net->start(cfg); |
nyatla | 25:1a4f620b7af6 | 41 | |
nyatla | 25:1a4f620b7af6 | 42 | |
nyatla | 25:1a4f620b7af6 | 43 | led1=1; |
nyatla | 25:1a4f620b7af6 | 44 | for(;;){ |
nyatla | 25:1a4f620b7af6 | 45 | //connect to server |
nyatla | 25:1a4f620b7af6 | 46 | if(!socket.connect(IpAddr(192,168,128,195),1234)){ |
nyatla | 25:1a4f620b7af6 | 47 | Thread::wait(1000); |
nyatla | 25:1a4f620b7af6 | 48 | } |
nyatla | 25:1a4f620b7af6 | 49 | //connected! |
nyatla | 25:1a4f620b7af6 | 50 | led2=1; |
nyatla | 25:1a4f620b7af6 | 51 | for(;;){ |
nyatla | 25:1a4f620b7af6 | 52 | led4=0; |
nyatla | 25:1a4f620b7af6 | 53 | led3=1; |
nyatla | 25:1a4f620b7af6 | 54 | //wait for data... |
nyatla | 25:1a4f620b7af6 | 55 | const void* rx; |
nyatla | 25:1a4f620b7af6 | 56 | //get read pointer |
nyatla | 25:1a4f620b7af6 | 57 | int l=socket.precv(rx); |
nyatla | 25:1a4f620b7af6 | 58 | if(l<0){ |
nyatla | 25:1a4f620b7af6 | 59 | break; |
nyatla | 25:1a4f620b7af6 | 60 | } |
nyatla | 25:1a4f620b7af6 | 61 | if(l==0){ |
nyatla | 25:1a4f620b7af6 | 62 | //timeout |
nyatla | 25:1a4f620b7af6 | 63 | }else{ |
nyatla | 25:1a4f620b7af6 | 64 | //ok,echo back data. |
nyatla | 25:1a4f620b7af6 | 65 | led4=1; |
nyatla | 25:1a4f620b7af6 | 66 | //send data |
nyatla | 25:1a4f620b7af6 | 67 | if(!socket.send(rx,l)){ |
nyatla | 25:1a4f620b7af6 | 68 | break; |
nyatla | 25:1a4f620b7af6 | 69 | } |
nyatla | 25:1a4f620b7af6 | 70 | //move read pointer. |
nyatla | 25:1a4f620b7af6 | 71 | socket.pseek(l); |
nyatla | 25:1a4f620b7af6 | 72 | } |
nyatla | 25:1a4f620b7af6 | 73 | led3=0; |
nyatla | 25:1a4f620b7af6 | 74 | } |
nyatla | 25:1a4f620b7af6 | 75 | led2=0; |
nyatla | 25:1a4f620b7af6 | 76 | led3=0; |
nyatla | 25:1a4f620b7af6 | 77 | led4=0; |
nyatla | 25:1a4f620b7af6 | 78 | socket.close(); //close the socket. |
nyatla | 25:1a4f620b7af6 | 79 | } |
nyatla | 0:ec1e45489427 | 80 | return 0; |
nyatla | 0:ec1e45489427 | 81 | } |
nyatla | 25:1a4f620b7af6 | 82 |