A basic example of two treads for TCP and UDP. The TCP server in its thread waiting for a command (!start or !stop) from a client. That control the UDP thread and it start or stop sending a dummy message.
Diff: main.cpp
- Revision:
- 1:191f7e703c43
- Parent:
- 0:d54540de63d4
diff -r d54540de63d4 -r 191f7e703c43 main.cpp --- a/main.cpp Fri May 29 11:10:56 2020 +0000 +++ b/main.cpp Fri May 29 11:49:27 2020 +0000 @@ -22,12 +22,63 @@ DigitalOut led3(LED3); Thread threadUDP; Thread threadTCP; +Ticker ticker; NetworkInterface *net = NetworkInterface::get_default_instance(); SocketAddress ip; volatile bool startFlag = false; - +volatile int dummyValue = 0; -void serverTCP(){ +void tickerFunc(); +void taskTCP(); +void taskUDP(); + +int main() { + printf("Example of UDP and TCP at once\n"); + int net_stat; +#ifndef ROUTER + net->disconnect(); + net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY); + printf("Network set IP status: %s\n", net_stat ? "Error": "OK"); +#endif +printf("Connecting..."); + net_stat = net->connect(); + printf("%s\n",net_stat ? "Error": "OK"); + net->get_ip_address(&ip); + const char *p_ip = ip.get_ip_address(); + printf("Device IP address: %s\n", p_ip ? p_ip : "None"); + if(ip){ + SocketAddress mask; + net->get_netmask(&mask); + const char *p_mask = mask.get_ip_address(); + printf("Netmask: %s\n", p_mask ? p_mask : "None"); + SocketAddress gateway; + net->get_gateway(&gateway); + const char *p_gateway = gateway.get_ip_address(); + printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); + ticker.attach(&tickerFunc, 1); + printf("Starting of TCP and UDP threads...\n"); + ThisThread::sleep_for(200); + threadUDP.start(callback(taskUDP)); + ThisThread::sleep_for(200); + threadTCP.start(callback(taskTCP)); + ThisThread::sleep_for(200); + }else{ + printf("No IP\n"); + if(net != nullptr) net->disconnect(); + printf("Program End\n"); + } + + while(1) { + led1 =! led1; + ThisThread::sleep_for(1000); + } +} + +void tickerFunc(){ + dummyValue = rand() % 100; //random 0-99 +} + +void taskTCP(){ printf("TCP Server starting...\n"); TCPSocket server; TCPSocket *client; @@ -69,7 +120,7 @@ printf("Thread end\n"); } -void udpEcho(){ +void taskUDP(){ printf("UDP starting...\n"); UDPSocket sock; SocketAddress addr; @@ -77,55 +128,13 @@ sock.open(net); sock.bind(UDPPORT); printf("UDP listens on the local port: %d and send to the remote port %d\n", UDPPORT, UDPREMOTEPORT); - int dummyValue = 0; char buffer[BUFFSIZE]; while(1){ while(!startFlag) ThisThread::sleep_for(100); - sprintf(buffer,"Dummy value %d\n",dummyValue++); + sprintf(buffer,"Dummy value %d\n",dummyValue); sock.sendto(targetAddr, buffer, sizeof(buffer)); printf("Send back: %s", buffer); led3 =! led3; ThisThread::sleep_for(500); } -} - -int main() { - printf("Example of UDP and TCP at once\n"); - int net_stat; -#ifndef ROUTER - net->disconnect(); - net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY); - printf("Network set IP status: %s\n", net_stat ? "Error": "OK"); -#endif -printf("Connecting..."); - net_stat = net->connect(); - printf("%s\n",net_stat ? "Error": "OK"); - net->get_ip_address(&ip); - const char *p_ip = ip.get_ip_address(); - printf("Device IP address: %s\n", p_ip ? p_ip : "None"); - if(ip){ - SocketAddress mask; - net->get_netmask(&mask); - const char *p_mask = mask.get_ip_address(); - printf("Netmask: %s\n", p_mask ? p_mask : "None"); - SocketAddress gateway; - net->get_gateway(&gateway); - const char *p_gateway = gateway.get_ip_address(); - printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); - printf("Starting of TCP and UDP threads...\n"); - ThisThread::sleep_for(200); - threadUDP.start(callback(udpEcho)); - ThisThread::sleep_for(200); - threadTCP.start(callback(serverTCP)); - ThisThread::sleep_for(200); - }else{ - printf("No IP\n"); - if(net != nullptr) net->disconnect(); - printf("Program End\n"); - } - - while(1) { - led1 =! led1; - ThisThread::sleep_for(1000); - } -} \ No newline at end of file +} \ No newline at end of file