Ethernet Voice Streaming code
Dependencies: WIZnetInterface mbed
Fork of UDPEchoServer_TEST by
main.cpp@8:47e9d7df0582, 2015-08-21 (annotated)
- Committer:
- eunkyoungkim
- Date:
- Fri Aug 21 03:34:25 2015 +0000
- Revision:
- 8:47e9d7df0582
- Parent:
- 7:345b078c448a
- Child:
- 9:2593d6010881
UDP Client...; - Add sock.bind
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:3e54841149df | 1 | #include "mbed.h" |
emilmont | 1:3f409cd0bede | 2 | #include "EthernetInterface.h" |
eunkyoungkim | 7:345b078c448a | 3 | #include "SDFileSystem.h" |
emilmont | 1:3f409cd0bede | 4 | |
eunkyoungkim | 7:345b078c448a | 5 | |
eunkyoungkim | 7:345b078c448a | 6 | //#define UDPServer 1 |
eunkyoungkim | 7:345b078c448a | 7 | #define UDPClinet 1 |
eunkyoungkim | 7:345b078c448a | 8 | |
eunkyoungkim | 7:345b078c448a | 9 | #if defined(UDPServer) |
eunkyoungkim | 7:345b078c448a | 10 | #define MAC "\x00\x08\xDC\x11\x34\x78" |
eunkyoungkim | 7:345b078c448a | 11 | #define IP "192.168.99.100" |
eunkyoungkim | 7:345b078c448a | 12 | #define MASK "255.255.255.0" |
eunkyoungkim | 7:345b078c448a | 13 | #define GATEWAY "192.168.99.1" |
eunkyoungkim | 7:345b078c448a | 14 | #endif |
eunkyoungkim | 7:345b078c448a | 15 | #if defined(UDPClinet) |
eunkyoungkim | 7:345b078c448a | 16 | #define MAC "\x00\x08\xDC\x11\x34\x90" |
eunkyoungkim | 7:345b078c448a | 17 | #define IP "192.168.99.101" |
eunkyoungkim | 7:345b078c448a | 18 | #define MASK "255.255.255.0" |
eunkyoungkim | 7:345b078c448a | 19 | #define GATEWAY "192.168.99.1" |
eunkyoungkim | 7:345b078c448a | 20 | #endif |
eunkyoungkim | 7:345b078c448a | 21 | const char* ECHO_SERVER_ADDRESS = "192.168.99.100"; |
eunkyoungkim | 8:47e9d7df0582 | 22 | const int ECHO_SERVER_PORT = 3000; |
eunkyoungkim | 8:47e9d7df0582 | 23 | const int ECHO_CLIENT_PORT = 2000; |
emilmont | 1:3f409cd0bede | 24 | int main (void) { |
eunkyoungkim | 7:345b078c448a | 25 | |
emilmont | 1:3f409cd0bede | 26 | EthernetInterface eth; |
eunkyoungkim | 7:345b078c448a | 27 | Serial (USBTX, USBRX); |
eunkyoungkim | 7:345b078c448a | 28 | eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway |
eunkyoungkim | 7:345b078c448a | 29 | printf("Connecting\n"); |
emilmont | 1:3f409cd0bede | 30 | eth.connect(); |
eunkyoungkim | 7:345b078c448a | 31 | #if defined(UDPServer) |
eunkyoungkim | 7:345b078c448a | 32 | while (true) |
eunkyoungkim | 7:345b078c448a | 33 | { |
eunkyoungkim | 7:345b078c448a | 34 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
eunkyoungkim | 7:345b078c448a | 35 | UDPSocket server; |
eunkyoungkim | 7:345b078c448a | 36 | server.bind(ECHO_SERVER_PORT); |
emilmont | 1:3f409cd0bede | 37 | |
eunkyoungkim | 7:345b078c448a | 38 | Endpoint client; |
eunkyoungkim | 7:345b078c448a | 39 | char buffer[256]; |
eunkyoungkim | 7:345b078c448a | 40 | while (true) { |
eunkyoungkim | 7:345b078c448a | 41 | printf("\nWaiting for UDP packet...\n"); |
eunkyoungkim | 7:345b078c448a | 42 | int n = server.receiveFrom(client, buffer, sizeof(buffer)); |
eunkyoungkim | 7:345b078c448a | 43 | if (n <= 0) break; |
eunkyoungkim | 7:345b078c448a | 44 | // print received message to terminal |
eunkyoungkim | 7:345b078c448a | 45 | //buffer[n] = '\0'; |
eunkyoungkim | 7:345b078c448a | 46 | |
eunkyoungkim | 7:345b078c448a | 47 | printf("Received packet from: %s\n", client.get_address()); |
eunkyoungkim | 7:345b078c448a | 48 | printf("Packet contents : '%s'\n",buffer); |
eunkyoungkim | 7:345b078c448a | 49 | printf("Sending Packet back to Client\n"); |
eunkyoungkim | 7:345b078c448a | 50 | server.sendTo(client, buffer, n); |
eunkyoungkim | 7:345b078c448a | 51 | if (n <= 0) break; |
eunkyoungkim | 7:345b078c448a | 52 | } |
emilmont | 1:3f409cd0bede | 53 | } |
eunkyoungkim | 7:345b078c448a | 54 | #else //Client |
eunkyoungkim | 7:345b078c448a | 55 | printf("\nClient IP Address is %s \n", eth.getIPAddress()); |
eunkyoungkim | 7:345b078c448a | 56 | |
eunkyoungkim | 7:345b078c448a | 57 | UDPSocket sock; |
eunkyoungkim | 7:345b078c448a | 58 | sock.init(); |
eunkyoungkim | 8:47e9d7df0582 | 59 | sock.bind(ECHO_CLIENT_PORT); |
eunkyoungkim | 7:345b078c448a | 60 | Endpoint echo_server; |
eunkyoungkim | 7:345b078c448a | 61 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
eunkyoungkim | 7:345b078c448a | 62 | |
eunkyoungkim | 7:345b078c448a | 63 | char out_buffer[] = "Hello World"; |
eunkyoungkim | 7:345b078c448a | 64 | printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); |
eunkyoungkim | 8:47e9d7df0582 | 65 | printf("ip= %s,port=%d\r\n",echo_server.get_address(),echo_server.get_port()); |
eunkyoungkim | 7:345b078c448a | 66 | sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); |
eunkyoungkim | 7:345b078c448a | 67 | |
eunkyoungkim | 7:345b078c448a | 68 | char in_buffer[256]; |
eunkyoungkim | 8:47e9d7df0582 | 69 | // int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer)); |
eunkyoungkim | 7:345b078c448a | 70 | |
eunkyoungkim | 8:47e9d7df0582 | 71 | // in_buffer[n] = '\0'; |
eunkyoungkim | 8:47e9d7df0582 | 72 | //printf("Received message from server: '%s'\n", in_buffer); |
eunkyoungkim | 7:345b078c448a | 73 | |
eunkyoungkim | 7:345b078c448a | 74 | sock.close(); |
eunkyoungkim | 7:345b078c448a | 75 | |
eunkyoungkim | 7:345b078c448a | 76 | eth.disconnect(); |
eunkyoungkim | 7:345b078c448a | 77 | while(1) {} |
eunkyoungkim | 7:345b078c448a | 78 | #endif |
emilmont | 1:3f409cd0bede | 79 | } |