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.

Committer:
JohnnyK
Date:
Fri May 29 11:49:27 2020 +0000
Revision:
1:191f7e703c43
Parent:
0:d54540de63d4
Add ticker function for generate random value outside of UDP thread.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 0:d54540de63d4 1 #include "mbed.h" //MbedOS 5.15 on Nucleo-F767ZI
JohnnyK 0:d54540de63d4 2 #include "NetworkInterface.h"
JohnnyK 0:d54540de63d4 3
JohnnyK 0:d54540de63d4 4 #define BUFFSIZE 50
JohnnyK 0:d54540de63d4 5
JohnnyK 0:d54540de63d4 6 #define ROUTER
JohnnyK 0:d54540de63d4 7 #ifndef ROUTER
JohnnyK 0:d54540de63d4 8 #define IP "192.168.1.1" //Here place your Static IP of Mbed, when you want it to connect directly to PC
JohnnyK 0:d54540de63d4 9 #define GATEWAY "0.0.0.0"
JohnnyK 0:d54540de63d4 10 #define MASK "255.255.255.0"
JohnnyK 0:d54540de63d4 11 #endif
JohnnyK 0:d54540de63d4 12 #define ADDRESS "192.168.1.10" /*Here place IP of your PC. Run cmd.exe and write command ipconfig.
JohnnyK 0:d54540de63d4 13 If the address will be incorrect, the first message will be lost
JohnnyK 0:d54540de63d4 14 and connection will be established with first received message from opposite side.*/
JohnnyK 0:d54540de63d4 15 #define UDPPORT 20
JohnnyK 0:d54540de63d4 16 #define UDPREMOTEPORT 2000
JohnnyK 0:d54540de63d4 17 #define TCPPORT 80
JohnnyK 0:d54540de63d4 18
JohnnyK 0:d54540de63d4 19
JohnnyK 0:d54540de63d4 20 DigitalOut led1(LED1);
JohnnyK 0:d54540de63d4 21 DigitalOut led2(LED2);
JohnnyK 0:d54540de63d4 22 DigitalOut led3(LED3);
JohnnyK 0:d54540de63d4 23 Thread threadUDP;
JohnnyK 0:d54540de63d4 24 Thread threadTCP;
JohnnyK 1:191f7e703c43 25 Ticker ticker;
JohnnyK 0:d54540de63d4 26 NetworkInterface *net = NetworkInterface::get_default_instance();
JohnnyK 0:d54540de63d4 27 SocketAddress ip;
JohnnyK 0:d54540de63d4 28 volatile bool startFlag = false;
JohnnyK 1:191f7e703c43 29 volatile int dummyValue = 0;
JohnnyK 0:d54540de63d4 30
JohnnyK 1:191f7e703c43 31 void tickerFunc();
JohnnyK 1:191f7e703c43 32 void taskTCP();
JohnnyK 1:191f7e703c43 33 void taskUDP();
JohnnyK 1:191f7e703c43 34
JohnnyK 1:191f7e703c43 35 int main() {
JohnnyK 1:191f7e703c43 36 printf("Example of UDP and TCP at once\n");
JohnnyK 1:191f7e703c43 37 int net_stat;
JohnnyK 1:191f7e703c43 38 #ifndef ROUTER
JohnnyK 1:191f7e703c43 39 net->disconnect();
JohnnyK 1:191f7e703c43 40 net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY);
JohnnyK 1:191f7e703c43 41 printf("Network set IP status: %s\n", net_stat ? "Error": "OK");
JohnnyK 1:191f7e703c43 42 #endif
JohnnyK 1:191f7e703c43 43 printf("Connecting...");
JohnnyK 1:191f7e703c43 44 net_stat = net->connect();
JohnnyK 1:191f7e703c43 45 printf("%s\n",net_stat ? "Error": "OK");
JohnnyK 1:191f7e703c43 46 net->get_ip_address(&ip);
JohnnyK 1:191f7e703c43 47 const char *p_ip = ip.get_ip_address();
JohnnyK 1:191f7e703c43 48 printf("Device IP address: %s\n", p_ip ? p_ip : "None");
JohnnyK 1:191f7e703c43 49 if(ip){
JohnnyK 1:191f7e703c43 50 SocketAddress mask;
JohnnyK 1:191f7e703c43 51 net->get_netmask(&mask);
JohnnyK 1:191f7e703c43 52 const char *p_mask = mask.get_ip_address();
JohnnyK 1:191f7e703c43 53 printf("Netmask: %s\n", p_mask ? p_mask : "None");
JohnnyK 1:191f7e703c43 54 SocketAddress gateway;
JohnnyK 1:191f7e703c43 55 net->get_gateway(&gateway);
JohnnyK 1:191f7e703c43 56 const char *p_gateway = gateway.get_ip_address();
JohnnyK 1:191f7e703c43 57 printf("Gateway: %s\n", p_gateway ? p_gateway : "None");
JohnnyK 1:191f7e703c43 58 ticker.attach(&tickerFunc, 1);
JohnnyK 1:191f7e703c43 59 printf("Starting of TCP and UDP threads...\n");
JohnnyK 1:191f7e703c43 60 ThisThread::sleep_for(200);
JohnnyK 1:191f7e703c43 61 threadUDP.start(callback(taskUDP));
JohnnyK 1:191f7e703c43 62 ThisThread::sleep_for(200);
JohnnyK 1:191f7e703c43 63 threadTCP.start(callback(taskTCP));
JohnnyK 1:191f7e703c43 64 ThisThread::sleep_for(200);
JohnnyK 1:191f7e703c43 65 }else{
JohnnyK 1:191f7e703c43 66 printf("No IP\n");
JohnnyK 1:191f7e703c43 67 if(net != nullptr) net->disconnect();
JohnnyK 1:191f7e703c43 68 printf("Program End\n");
JohnnyK 1:191f7e703c43 69 }
JohnnyK 1:191f7e703c43 70
JohnnyK 1:191f7e703c43 71 while(1) {
JohnnyK 1:191f7e703c43 72 led1 =! led1;
JohnnyK 1:191f7e703c43 73 ThisThread::sleep_for(1000);
JohnnyK 1:191f7e703c43 74 }
JohnnyK 1:191f7e703c43 75 }
JohnnyK 1:191f7e703c43 76
JohnnyK 1:191f7e703c43 77 void tickerFunc(){
JohnnyK 1:191f7e703c43 78 dummyValue = rand() % 100; //random 0-99
JohnnyK 1:191f7e703c43 79 }
JohnnyK 1:191f7e703c43 80
JohnnyK 1:191f7e703c43 81 void taskTCP(){
JohnnyK 0:d54540de63d4 82 printf("TCP Server starting...\n");
JohnnyK 0:d54540de63d4 83 TCPSocket server;
JohnnyK 0:d54540de63d4 84 TCPSocket *client;
JohnnyK 0:d54540de63d4 85 SocketAddress clientAddress;
JohnnyK 0:d54540de63d4 86 char in_buffer[BUFFSIZE];
JohnnyK 0:d54540de63d4 87 const char *selection[] = {"!stop","!start"};
JohnnyK 0:d54540de63d4 88 server.open(net);
JohnnyK 0:d54540de63d4 89 server.bind(TCPPORT);
JohnnyK 0:d54540de63d4 90 server.listen(1);
JohnnyK 0:d54540de63d4 91 printf("TCP Server bound and listening at port: %d\n", TCPPORT);
JohnnyK 0:d54540de63d4 92 while (1) {
JohnnyK 0:d54540de63d4 93 client = server.accept();
JohnnyK 0:d54540de63d4 94 client->getpeername(&clientAddress);
JohnnyK 0:d54540de63d4 95 const char *p_clientAddress = clientAddress.get_ip_address();
JohnnyK 0:d54540de63d4 96 printf("Client connected from IP address: %s\n", p_clientAddress ? p_clientAddress : "None");
JohnnyK 0:d54540de63d4 97 bool b = true;
JohnnyK 0:d54540de63d4 98 while (b) {
JohnnyK 0:d54540de63d4 99 led2 =! led2;
JohnnyK 0:d54540de63d4 100 int n = client->recv(in_buffer, BUFFSIZE);
JohnnyK 0:d54540de63d4 101 if (n == 0) {
JohnnyK 0:d54540de63d4 102 printf("Client disconnected\n");
JohnnyK 0:d54540de63d4 103 b = false;
JohnnyK 0:d54540de63d4 104 }else{
JohnnyK 0:d54540de63d4 105 in_buffer[n] = '\0';
JohnnyK 0:d54540de63d4 106 char *result = nullptr;
JohnnyK 0:d54540de63d4 107 for(int i = 0; i<2; i++){
JohnnyK 0:d54540de63d4 108 result = strstr(in_buffer,selection[i]);
JohnnyK 0:d54540de63d4 109 if(result != nullptr){
JohnnyK 0:d54540de63d4 110 startFlag = i;
JohnnyK 0:d54540de63d4 111 break;
JohnnyK 0:d54540de63d4 112 }
JohnnyK 0:d54540de63d4 113 }
JohnnyK 0:d54540de63d4 114 printf("Received message from Client :'%s'\n", in_buffer);
JohnnyK 0:d54540de63d4 115 }
JohnnyK 0:d54540de63d4 116 }
JohnnyK 0:d54540de63d4 117 client->close();
JohnnyK 0:d54540de63d4 118 }
JohnnyK 0:d54540de63d4 119 server.close();
JohnnyK 0:d54540de63d4 120 printf("Thread end\n");
JohnnyK 0:d54540de63d4 121 }
JohnnyK 0:d54540de63d4 122
JohnnyK 1:191f7e703c43 123 void taskUDP(){
JohnnyK 0:d54540de63d4 124 printf("UDP starting...\n");
JohnnyK 0:d54540de63d4 125 UDPSocket sock;
JohnnyK 0:d54540de63d4 126 SocketAddress addr;
JohnnyK 0:d54540de63d4 127 SocketAddress targetAddr(ADDRESS,UDPREMOTEPORT);
JohnnyK 0:d54540de63d4 128 sock.open(net);
JohnnyK 0:d54540de63d4 129 sock.bind(UDPPORT);
JohnnyK 0:d54540de63d4 130 printf("UDP listens on the local port: %d and send to the remote port %d\n", UDPPORT, UDPREMOTEPORT);
JohnnyK 0:d54540de63d4 131 char buffer[BUFFSIZE];
JohnnyK 0:d54540de63d4 132 while(1){
JohnnyK 0:d54540de63d4 133 while(!startFlag) ThisThread::sleep_for(100);
JohnnyK 1:191f7e703c43 134 sprintf(buffer,"Dummy value %d\n",dummyValue);
JohnnyK 0:d54540de63d4 135 sock.sendto(targetAddr, buffer, sizeof(buffer));
JohnnyK 0:d54540de63d4 136 printf("Send back: %s", buffer);
JohnnyK 0:d54540de63d4 137 led3 =! led3;
JohnnyK 0:d54540de63d4 138 ThisThread::sleep_for(500);
JohnnyK 0:d54540de63d4 139 }
JohnnyK 1:191f7e703c43 140 }