Jan Kamidra
/
UDP_Echo
UDP echo with thread
main.cpp@2:01b03970605d, 2019-07-03 (annotated)
- Committer:
- JohnnyK
- Date:
- Wed Jul 03 05:41:00 2019 +0000
- Revision:
- 2:01b03970605d
- Parent:
- 1:9c712ebb90d3
- Child:
- 3:9ab431fc54e2
Rework - switch between auto IP or Static IP via definition ROUTER
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 2:01b03970605d | 1 | #include "mbed.h" //MbedOS 5.13 |
JohnnyK | 0:9f56faada164 | 2 | #include "EthernetInterface.h" |
JohnnyK | 0:9f56faada164 | 3 | |
JohnnyK | 2:01b03970605d | 4 | #define ADDRESS "192.168.1.10" //Here place IP of your PC. Run cmd.exe and write command ipconfig |
JohnnyK | 2:01b03970605d | 5 | #define LOCALPORT 20 |
JohnnyK | 2:01b03970605d | 6 | #define REMOTEPORT 2000 |
JohnnyK | 2:01b03970605d | 7 | |
JohnnyK | 2:01b03970605d | 8 | #define ROUTER |
JohnnyK | 2:01b03970605d | 9 | #ifndef ROUTER |
JohnnyK | 2:01b03970605d | 10 | #define IP "192.168.1.1" |
JohnnyK | 2:01b03970605d | 11 | #define GATEWAY "0.0.0.0" |
JohnnyK | 2:01b03970605d | 12 | #define MASK "255.255.255.0" |
JohnnyK | 2:01b03970605d | 13 | #endif |
JohnnyK | 0:9f56faada164 | 14 | |
JohnnyK | 0:9f56faada164 | 15 | Thread thread; |
JohnnyK | 0:9f56faada164 | 16 | DigitalOut led1(LED1); |
JohnnyK | 0:9f56faada164 | 17 | DigitalOut led2(LED2); |
JohnnyK | 0:9f56faada164 | 18 | |
JohnnyK | 0:9f56faada164 | 19 | void udpEcho(){ |
JohnnyK | 0:9f56faada164 | 20 | EthernetInterface eth; |
JohnnyK | 0:9f56faada164 | 21 | UDPSocket sock; |
JohnnyK | 2:01b03970605d | 22 | SocketAddress addr(0,LOCALPORT); |
JohnnyK | 2:01b03970605d | 23 | int eth_stat; |
JohnnyK | 2:01b03970605d | 24 | #ifndef ROUTER |
JohnnyK | 2:01b03970605d | 25 | eth.disconnect(); |
JohnnyK | 2:01b03970605d | 26 | eth_stat = eth.set_network(IP,MASK,GATEWAY); |
JohnnyK | 2:01b03970605d | 27 | printf("set IP status: %i\n",eth_stat); |
JohnnyK | 2:01b03970605d | 28 | #endif |
JohnnyK | 2:01b03970605d | 29 | eth_stat = eth.connect(); |
JohnnyK | 2:01b03970605d | 30 | printf("connect status: %i\n",eth_stat); |
JohnnyK | 0:9f56faada164 | 31 | const char *ip = eth.get_ip_address(); |
JohnnyK | 2:01b03970605d | 32 | const char *mac = eth.get_mac_address(); |
JohnnyK | 2:01b03970605d | 33 | printf("MAC address is: %s\n", mac ? mac : "No MAC"); |
JohnnyK | 2:01b03970605d | 34 | printf("IP address is: %s\n", ip ? ip : "No IP"); |
JohnnyK | 0:9f56faada164 | 35 | // Check ip |
JohnnyK | 2:01b03970605d | 36 | if(ip){// Check ip |
JohnnyK | 0:9f56faada164 | 37 | sock.open(ð); |
JohnnyK | 2:01b03970605d | 38 | sock.bind(LOCALPORT); |
JohnnyK | 2:01b03970605d | 39 | printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT); |
JohnnyK | 0:9f56faada164 | 40 | char buffer[] = "Hello World, UDP Echo server"; |
JohnnyK | 0:9f56faada164 | 41 | printf("Sending message '%s' to server (%s)\n",buffer,ip); |
JohnnyK | 2:01b03970605d | 42 | sock.sendto(ADDRESS, REMOTEPORT, buffer, sizeof(buffer)); |
JohnnyK | 0:9f56faada164 | 43 | printf("Waiting for message...\n"); |
JohnnyK | 0:9f56faada164 | 44 | |
JohnnyK | 0:9f56faada164 | 45 | while(1) { |
JohnnyK | 0:9f56faada164 | 46 | char in_buffer[80]; |
JohnnyK | 0:9f56faada164 | 47 | int n = sock.recvfrom(&addr, &in_buffer, sizeof(in_buffer)); |
JohnnyK | 0:9f56faada164 | 48 | in_buffer[n] = '\0'; |
JohnnyK | 0:9f56faada164 | 49 | printf("Recieved: %s\n", in_buffer); |
JohnnyK | 0:9f56faada164 | 50 | |
JohnnyK | 0:9f56faada164 | 51 | char out_buffer[80]; |
JohnnyK | 0:9f56faada164 | 52 | n = sprintf(out_buffer,"Echo - %s", in_buffer); |
JohnnyK | 0:9f56faada164 | 53 | out_buffer[n] = '\0'; |
JohnnyK | 0:9f56faada164 | 54 | printf("Send back: %s\n", out_buffer); |
JohnnyK | 2:01b03970605d | 55 | sock.sendto(addr.get_ip_address(), REMOTEPORT, out_buffer, sizeof(out_buffer)); |
JohnnyK | 2:01b03970605d | 56 | printf("IP address is: %s\n", addr.get_ip_address()); |
JohnnyK | 0:9f56faada164 | 57 | led2 =! led2; |
JohnnyK | 0:9f56faada164 | 58 | } |
JohnnyK | 0:9f56faada164 | 59 | /*sock.close(); |
JohnnyK | 0:9f56faada164 | 60 | eth.disconnect(); */ |
JohnnyK | 0:9f56faada164 | 61 | }else{ |
JohnnyK | 0:9f56faada164 | 62 | printf("No IP\n"); |
JohnnyK | 0:9f56faada164 | 63 | //eth.disconnect(); |
JohnnyK | 0:9f56faada164 | 64 | printf("Thread end\n"); |
JohnnyK | 0:9f56faada164 | 65 | } |
JohnnyK | 0:9f56faada164 | 66 | } |
JohnnyK | 0:9f56faada164 | 67 | |
JohnnyK | 0:9f56faada164 | 68 | |
JohnnyK | 0:9f56faada164 | 69 | int main() { |
JohnnyK | 1:9c712ebb90d3 | 70 | printf("UDP echo starting...\n"); |
JohnnyK | 0:9f56faada164 | 71 | thread.start(callback(udpEcho)); |
JohnnyK | 0:9f56faada164 | 72 | wait(1); |
JohnnyK | 0:9f56faada164 | 73 | |
JohnnyK | 0:9f56faada164 | 74 | while(1) { |
JohnnyK | 0:9f56faada164 | 75 | led1 =! led1; |
JohnnyK | 0:9f56faada164 | 76 | wait(1); |
JohnnyK | 0:9f56faada164 | 77 | } |
JohnnyK | 0:9f56faada164 | 78 | } |