Jan Kamidra
/
UDP_Echo
UDP echo with thread
Diff: main.cpp
- Revision:
- 3:9ab431fc54e2
- Parent:
- 2:01b03970605d
- Child:
- 4:2579ef3b025f
--- a/main.cpp Wed Jul 03 05:41:00 2019 +0000 +++ b/main.cpp Fri May 08 12:03:27 2020 +0000 @@ -1,25 +1,23 @@ -#include "mbed.h" //MbedOS 5.13 +#include "mbed.h" //MbedOS 5.15 #include "EthernetInterface.h" -#define ADDRESS "192.168.1.10" //Here place IP of your PC. Run cmd.exe and write command ipconfig -#define LOCALPORT 20 -#define REMOTEPORT 2000 - #define ROUTER #ifndef ROUTER - #define IP "192.168.1.1" - #define GATEWAY "0.0.0.0" - #define MASK "255.255.255.0" + #define IP "192.168.1.1" //Here place your defined IP of Mbed + #define GATEWAY "0.0.0.0" + #define MASK "255.255.255.0" #endif +#define ADDRESS "10.0.1.29" //Here place IP of your PC. Run cmd.exe and write command ipconfig +#define LOCALPORT 20 +#define REMOTEPORT 2000 Thread thread; DigitalOut led1(LED1); DigitalOut led2(LED2); +SocketAddress targetAddr(ADDRESS,REMOTEPORT); void udpEcho(){ EthernetInterface eth; - UDPSocket sock; - SocketAddress addr(0,LOCALPORT); int eth_stat; #ifndef ROUTER eth.disconnect(); @@ -28,18 +26,30 @@ #endif eth_stat = eth.connect(); printf("connect status: %i\n",eth_stat); - const char *ip = eth.get_ip_address(); - const char *mac = eth.get_mac_address(); - printf("MAC address is: %s\n", mac ? mac : "No MAC"); - printf("IP address is: %s\n", ip ? ip : "No IP"); + + SocketAddress ip; + eth.get_ip_address(&ip); + const char *p_ip = ip.get_ip_address(); + printf("IP address: %s\n", p_ip ? p_ip : "None"); + SocketAddress mask; + eth.get_netmask(&mask); + const char *p_mask = mask.get_ip_address(); + printf("Netmask: %s\n", p_mask ? p_mask : "None"); + SocketAddress gateway; + eth.get_gateway(&gateway); + const char *p_gateway = gateway.get_ip_address(); + printf("Gateway: %s\n", p_gateway ? p_gateway : "None"); + // Check ip if(ip){// Check ip + UDPSocket sock; + SocketAddress addr; sock.open(ð); sock.bind(LOCALPORT); printf("Listen on local port: %d and send to remote port %d\n", LOCALPORT, REMOTEPORT); - char buffer[] = "Hello World, UDP Echo server"; - printf("Sending message '%s' to server (%s)\n",buffer,ip); - sock.sendto(ADDRESS, REMOTEPORT, buffer, sizeof(buffer)); + char buffer[] = "Hello World, UDP Echo"; + printf("Sending message '%s' to (%s)\n",buffer,targetAddr.get_ip_address()); + sock.sendto(targetAddr, buffer, sizeof(buffer)); printf("Waiting for message...\n"); while(1) { @@ -47,17 +57,13 @@ int n = sock.recvfrom(&addr, &in_buffer, sizeof(in_buffer)); in_buffer[n] = '\0'; printf("Recieved: %s\n", in_buffer); - char out_buffer[80]; n = sprintf(out_buffer,"Echo - %s", in_buffer); out_buffer[n] = '\0'; printf("Send back: %s\n", out_buffer); - sock.sendto(addr.get_ip_address(), REMOTEPORT, out_buffer, sizeof(out_buffer)); - printf("IP address is: %s\n", addr.get_ip_address()); + sock.sendto(targetAddr, out_buffer, sizeof(out_buffer)); led2 =! led2; } - /*sock.close(); - eth.disconnect(); */ }else{ printf("No IP\n"); //eth.disconnect(); @@ -69,10 +75,10 @@ int main() { printf("UDP echo starting...\n"); thread.start(callback(udpEcho)); - wait(1); + ThisThread::sleep_for(1000); while(1) { led1 =! led1; - wait(1); + ThisThread::sleep_for(1000); } } \ No newline at end of file