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.
main.cpp@0:d54540de63d4, 2020-05-29 (annotated)
- Committer:
- JohnnyK
- Date:
- Fri May 29 11:10:56 2020 +0000
- Revision:
- 0:d54540de63d4
- Child:
- 1:191f7e703c43
First release
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:d54540de63d4 | 25 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
JohnnyK | 0:d54540de63d4 | 26 | SocketAddress ip; |
JohnnyK | 0:d54540de63d4 | 27 | volatile bool startFlag = false; |
JohnnyK | 0:d54540de63d4 | 28 | |
JohnnyK | 0:d54540de63d4 | 29 | |
JohnnyK | 0:d54540de63d4 | 30 | void serverTCP(){ |
JohnnyK | 0:d54540de63d4 | 31 | printf("TCP Server starting...\n"); |
JohnnyK | 0:d54540de63d4 | 32 | TCPSocket server; |
JohnnyK | 0:d54540de63d4 | 33 | TCPSocket *client; |
JohnnyK | 0:d54540de63d4 | 34 | SocketAddress clientAddress; |
JohnnyK | 0:d54540de63d4 | 35 | char in_buffer[BUFFSIZE]; |
JohnnyK | 0:d54540de63d4 | 36 | const char *selection[] = {"!stop","!start"}; |
JohnnyK | 0:d54540de63d4 | 37 | server.open(net); |
JohnnyK | 0:d54540de63d4 | 38 | server.bind(TCPPORT); |
JohnnyK | 0:d54540de63d4 | 39 | server.listen(1); |
JohnnyK | 0:d54540de63d4 | 40 | printf("TCP Server bound and listening at port: %d\n", TCPPORT); |
JohnnyK | 0:d54540de63d4 | 41 | while (1) { |
JohnnyK | 0:d54540de63d4 | 42 | client = server.accept(); |
JohnnyK | 0:d54540de63d4 | 43 | client->getpeername(&clientAddress); |
JohnnyK | 0:d54540de63d4 | 44 | const char *p_clientAddress = clientAddress.get_ip_address(); |
JohnnyK | 0:d54540de63d4 | 45 | printf("Client connected from IP address: %s\n", p_clientAddress ? p_clientAddress : "None"); |
JohnnyK | 0:d54540de63d4 | 46 | bool b = true; |
JohnnyK | 0:d54540de63d4 | 47 | while (b) { |
JohnnyK | 0:d54540de63d4 | 48 | led2 =! led2; |
JohnnyK | 0:d54540de63d4 | 49 | int n = client->recv(in_buffer, BUFFSIZE); |
JohnnyK | 0:d54540de63d4 | 50 | if (n == 0) { |
JohnnyK | 0:d54540de63d4 | 51 | printf("Client disconnected\n"); |
JohnnyK | 0:d54540de63d4 | 52 | b = false; |
JohnnyK | 0:d54540de63d4 | 53 | }else{ |
JohnnyK | 0:d54540de63d4 | 54 | in_buffer[n] = '\0'; |
JohnnyK | 0:d54540de63d4 | 55 | char *result = nullptr; |
JohnnyK | 0:d54540de63d4 | 56 | for(int i = 0; i<2; i++){ |
JohnnyK | 0:d54540de63d4 | 57 | result = strstr(in_buffer,selection[i]); |
JohnnyK | 0:d54540de63d4 | 58 | if(result != nullptr){ |
JohnnyK | 0:d54540de63d4 | 59 | startFlag = i; |
JohnnyK | 0:d54540de63d4 | 60 | break; |
JohnnyK | 0:d54540de63d4 | 61 | } |
JohnnyK | 0:d54540de63d4 | 62 | } |
JohnnyK | 0:d54540de63d4 | 63 | printf("Received message from Client :'%s'\n", in_buffer); |
JohnnyK | 0:d54540de63d4 | 64 | } |
JohnnyK | 0:d54540de63d4 | 65 | } |
JohnnyK | 0:d54540de63d4 | 66 | client->close(); |
JohnnyK | 0:d54540de63d4 | 67 | } |
JohnnyK | 0:d54540de63d4 | 68 | server.close(); |
JohnnyK | 0:d54540de63d4 | 69 | printf("Thread end\n"); |
JohnnyK | 0:d54540de63d4 | 70 | } |
JohnnyK | 0:d54540de63d4 | 71 | |
JohnnyK | 0:d54540de63d4 | 72 | void udpEcho(){ |
JohnnyK | 0:d54540de63d4 | 73 | printf("UDP starting...\n"); |
JohnnyK | 0:d54540de63d4 | 74 | UDPSocket sock; |
JohnnyK | 0:d54540de63d4 | 75 | SocketAddress addr; |
JohnnyK | 0:d54540de63d4 | 76 | SocketAddress targetAddr(ADDRESS,UDPREMOTEPORT); |
JohnnyK | 0:d54540de63d4 | 77 | sock.open(net); |
JohnnyK | 0:d54540de63d4 | 78 | sock.bind(UDPPORT); |
JohnnyK | 0:d54540de63d4 | 79 | printf("UDP listens on the local port: %d and send to the remote port %d\n", UDPPORT, UDPREMOTEPORT); |
JohnnyK | 0:d54540de63d4 | 80 | int dummyValue = 0; |
JohnnyK | 0:d54540de63d4 | 81 | char buffer[BUFFSIZE]; |
JohnnyK | 0:d54540de63d4 | 82 | while(1){ |
JohnnyK | 0:d54540de63d4 | 83 | while(!startFlag) ThisThread::sleep_for(100); |
JohnnyK | 0:d54540de63d4 | 84 | sprintf(buffer,"Dummy value %d\n",dummyValue++); |
JohnnyK | 0:d54540de63d4 | 85 | sock.sendto(targetAddr, buffer, sizeof(buffer)); |
JohnnyK | 0:d54540de63d4 | 86 | printf("Send back: %s", buffer); |
JohnnyK | 0:d54540de63d4 | 87 | led3 =! led3; |
JohnnyK | 0:d54540de63d4 | 88 | ThisThread::sleep_for(500); |
JohnnyK | 0:d54540de63d4 | 89 | } |
JohnnyK | 0:d54540de63d4 | 90 | } |
JohnnyK | 0:d54540de63d4 | 91 | |
JohnnyK | 0:d54540de63d4 | 92 | int main() { |
JohnnyK | 0:d54540de63d4 | 93 | printf("Example of UDP and TCP at once\n"); |
JohnnyK | 0:d54540de63d4 | 94 | int net_stat; |
JohnnyK | 0:d54540de63d4 | 95 | #ifndef ROUTER |
JohnnyK | 0:d54540de63d4 | 96 | net->disconnect(); |
JohnnyK | 0:d54540de63d4 | 97 | net_stat = net->set_network((SocketAddress)IP,(SocketAddress)MASK,(SocketAddress)GATEWAY); |
JohnnyK | 0:d54540de63d4 | 98 | printf("Network set IP status: %s\n", net_stat ? "Error": "OK"); |
JohnnyK | 0:d54540de63d4 | 99 | #endif |
JohnnyK | 0:d54540de63d4 | 100 | printf("Connecting..."); |
JohnnyK | 0:d54540de63d4 | 101 | net_stat = net->connect(); |
JohnnyK | 0:d54540de63d4 | 102 | printf("%s\n",net_stat ? "Error": "OK"); |
JohnnyK | 0:d54540de63d4 | 103 | net->get_ip_address(&ip); |
JohnnyK | 0:d54540de63d4 | 104 | const char *p_ip = ip.get_ip_address(); |
JohnnyK | 0:d54540de63d4 | 105 | printf("Device IP address: %s\n", p_ip ? p_ip : "None"); |
JohnnyK | 0:d54540de63d4 | 106 | if(ip){ |
JohnnyK | 0:d54540de63d4 | 107 | SocketAddress mask; |
JohnnyK | 0:d54540de63d4 | 108 | net->get_netmask(&mask); |
JohnnyK | 0:d54540de63d4 | 109 | const char *p_mask = mask.get_ip_address(); |
JohnnyK | 0:d54540de63d4 | 110 | printf("Netmask: %s\n", p_mask ? p_mask : "None"); |
JohnnyK | 0:d54540de63d4 | 111 | SocketAddress gateway; |
JohnnyK | 0:d54540de63d4 | 112 | net->get_gateway(&gateway); |
JohnnyK | 0:d54540de63d4 | 113 | const char *p_gateway = gateway.get_ip_address(); |
JohnnyK | 0:d54540de63d4 | 114 | printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); |
JohnnyK | 0:d54540de63d4 | 115 | printf("Starting of TCP and UDP threads...\n"); |
JohnnyK | 0:d54540de63d4 | 116 | ThisThread::sleep_for(200); |
JohnnyK | 0:d54540de63d4 | 117 | threadUDP.start(callback(udpEcho)); |
JohnnyK | 0:d54540de63d4 | 118 | ThisThread::sleep_for(200); |
JohnnyK | 0:d54540de63d4 | 119 | threadTCP.start(callback(serverTCP)); |
JohnnyK | 0:d54540de63d4 | 120 | ThisThread::sleep_for(200); |
JohnnyK | 0:d54540de63d4 | 121 | }else{ |
JohnnyK | 0:d54540de63d4 | 122 | printf("No IP\n"); |
JohnnyK | 0:d54540de63d4 | 123 | if(net != nullptr) net->disconnect(); |
JohnnyK | 0:d54540de63d4 | 124 | printf("Program End\n"); |
JohnnyK | 0:d54540de63d4 | 125 | } |
JohnnyK | 0:d54540de63d4 | 126 | |
JohnnyK | 0:d54540de63d4 | 127 | while(1) { |
JohnnyK | 0:d54540de63d4 | 128 | led1 =! led1; |
JohnnyK | 0:d54540de63d4 | 129 | ThisThread::sleep_for(1000); |
JohnnyK | 0:d54540de63d4 | 130 | } |
JohnnyK | 0:d54540de63d4 | 131 | } |