UDP Seerver LCinet

Dependencies:   SDFileSystem WIZnetInterface mbed

Fork of UDPEchoServer by Mbed

Committer:
eunkyoungkim
Date:
Fri Aug 21 02:25:23 2015 +0000
Revision:
7:345b078c448a
Parent:
6:bd7f46b6fa23
Child:
8:47e9d7df0582
UDP Server/Client

Who changed what in which revision?

UserRevisionLine numberNew 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 7:345b078c448a 22 const int ECHO_SERVER_PORT = 7;
emilmont 2:6b1012d93eb2 23
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 printf("g\n");
eunkyoungkim 7:345b078c448a 29 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); //IP,mask,Gateway
eunkyoungkim 7:345b078c448a 30 printf("Connecting\n");
emilmont 1:3f409cd0bede 31 eth.connect();
eunkyoungkim 7:345b078c448a 32 #if defined(UDPServer)
eunkyoungkim 7:345b078c448a 33 while (true)
eunkyoungkim 7:345b078c448a 34 {
eunkyoungkim 7:345b078c448a 35 printf("\nServer IP Address is %s\n", eth.getIPAddress());
eunkyoungkim 7:345b078c448a 36 UDPSocket server;
eunkyoungkim 7:345b078c448a 37 server.bind(ECHO_SERVER_PORT);
emilmont 1:3f409cd0bede 38
eunkyoungkim 7:345b078c448a 39 Endpoint client;
eunkyoungkim 7:345b078c448a 40 char buffer[256];
eunkyoungkim 7:345b078c448a 41 while (true) {
eunkyoungkim 7:345b078c448a 42 printf("\nWaiting for UDP packet...\n");
eunkyoungkim 7:345b078c448a 43 int n = server.receiveFrom(client, buffer, sizeof(buffer));
eunkyoungkim 7:345b078c448a 44 if (n <= 0) break;
eunkyoungkim 7:345b078c448a 45 // print received message to terminal
eunkyoungkim 7:345b078c448a 46 //buffer[n] = '\0';
eunkyoungkim 7:345b078c448a 47
eunkyoungkim 7:345b078c448a 48 printf("Received packet from: %s\n", client.get_address());
eunkyoungkim 7:345b078c448a 49 printf("Packet contents : '%s'\n",buffer);
eunkyoungkim 7:345b078c448a 50 printf("Sending Packet back to Client\n");
eunkyoungkim 7:345b078c448a 51 server.sendTo(client, buffer, n);
eunkyoungkim 7:345b078c448a 52 if (n <= 0) break;
eunkyoungkim 7:345b078c448a 53 }
emilmont 1:3f409cd0bede 54 }
eunkyoungkim 7:345b078c448a 55 #else //Client
eunkyoungkim 7:345b078c448a 56 printf("\nClient IP Address is %s \n", eth.getIPAddress());
eunkyoungkim 7:345b078c448a 57
eunkyoungkim 7:345b078c448a 58 UDPSocket sock;
eunkyoungkim 7:345b078c448a 59 sock.init();
eunkyoungkim 7:345b078c448a 60
eunkyoungkim 7:345b078c448a 61 Endpoint echo_server;
eunkyoungkim 7:345b078c448a 62 echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
eunkyoungkim 7:345b078c448a 63
eunkyoungkim 7:345b078c448a 64 char out_buffer[] = "Hello World";
eunkyoungkim 7:345b078c448a 65 printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS);
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 7:345b078c448a 69 int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
eunkyoungkim 7:345b078c448a 70
eunkyoungkim 7:345b078c448a 71 in_buffer[n] = '\0';
eunkyoungkim 7:345b078c448a 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 }
eunkyoungkim 7:345b078c448a 80