2
Dependencies: EthernetInterface2 mbed-rtos mbed-src2
main.cpp@0:2aa2e4bfb5ff, 2016-06-13 (annotated)
- Committer:
- zain_mbed
- Date:
- Mon Jun 13 09:19:37 2016 +0000
- Revision:
- 0:2aa2e4bfb5ff
5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zain_mbed | 0:2aa2e4bfb5ff | 1 | #include "mbed.h" |
zain_mbed | 0:2aa2e4bfb5ff | 2 | #include "EthernetInterface.h" |
zain_mbed | 0:2aa2e4bfb5ff | 3 | DigitalOut green(LED_GREEN); |
zain_mbed | 0:2aa2e4bfb5ff | 4 | DigitalOut red(LED_RED); |
zain_mbed | 0:2aa2e4bfb5ff | 5 | DigitalOut blue(LED_BLUE); |
zain_mbed | 0:2aa2e4bfb5ff | 6 | #define ECHO_SERVER_PORT 8089 |
zain_mbed | 0:2aa2e4bfb5ff | 7 | const char* ECHO_SERVER_ADDRESS = "192.168.68.250 "; |
zain_mbed | 0:2aa2e4bfb5ff | 8 | |
zain_mbed | 0:2aa2e4bfb5ff | 9 | |
zain_mbed | 0:2aa2e4bfb5ff | 10 | int main (void) { |
zain_mbed | 0:2aa2e4bfb5ff | 11 | EthernetInterface eth; |
zain_mbed | 0:2aa2e4bfb5ff | 12 | green=1; |
zain_mbed | 0:2aa2e4bfb5ff | 13 | red=1; |
zain_mbed | 0:2aa2e4bfb5ff | 14 | blue=1; |
zain_mbed | 0:2aa2e4bfb5ff | 15 | eth.init("192.168.68.93","255.255.255.0","192.168.68.1"); |
zain_mbed | 0:2aa2e4bfb5ff | 16 | eth.connect(); |
zain_mbed | 0:2aa2e4bfb5ff | 17 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
zain_mbed | 0:2aa2e4bfb5ff | 18 | green=1; |
zain_mbed | 0:2aa2e4bfb5ff | 19 | red=0; |
zain_mbed | 0:2aa2e4bfb5ff | 20 | blue=1; |
zain_mbed | 0:2aa2e4bfb5ff | 21 | UDPSocket server; |
zain_mbed | 0:2aa2e4bfb5ff | 22 | server.bind(ECHO_SERVER_PORT); |
zain_mbed | 0:2aa2e4bfb5ff | 23 | |
zain_mbed | 0:2aa2e4bfb5ff | 24 | Endpoint client; |
zain_mbed | 0:2aa2e4bfb5ff | 25 | Endpoint echo_server; |
zain_mbed | 0:2aa2e4bfb5ff | 26 | |
zain_mbed | 0:2aa2e4bfb5ff | 27 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
zain_mbed | 0:2aa2e4bfb5ff | 28 | |
zain_mbed | 0:2aa2e4bfb5ff | 29 | while (true) { |
zain_mbed | 0:2aa2e4bfb5ff | 30 | char buffer[1000]; |
zain_mbed | 0:2aa2e4bfb5ff | 31 | green=1; |
zain_mbed | 0:2aa2e4bfb5ff | 32 | red=0; |
zain_mbed | 0:2aa2e4bfb5ff | 33 | blue=1; |
zain_mbed | 0:2aa2e4bfb5ff | 34 | printf("\nWaiting for UDP packet...\n"); |
zain_mbed | 0:2aa2e4bfb5ff | 35 | int n = server.receiveFrom(client, buffer, sizeof(buffer)); |
zain_mbed | 0:2aa2e4bfb5ff | 36 | buffer[n] = '\0'; |
zain_mbed | 0:2aa2e4bfb5ff | 37 | green=0; |
zain_mbed | 0:2aa2e4bfb5ff | 38 | red=1; |
zain_mbed | 0:2aa2e4bfb5ff | 39 | blue=1; |
zain_mbed | 0:2aa2e4bfb5ff | 40 | printf("Received packet from: %s\n", client.get_address()); |
zain_mbed | 0:2aa2e4bfb5ff | 41 | printf("Packet contents : '%s'\n",buffer); |
zain_mbed | 0:2aa2e4bfb5ff | 42 | printf("Sending Packet back to Client\n"); |
zain_mbed | 0:2aa2e4bfb5ff | 43 | server.sendTo(echo_server, buffer, n); |
zain_mbed | 0:2aa2e4bfb5ff | 44 | } |
zain_mbed | 0:2aa2e4bfb5ff | 45 | } |