make the mbed become the udp server to receive the data from the client.

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of UDPEchoServer by mbed official

Files at this revision

API Documentation at this revision

Comitter:
shiyilei
Date:
Thu Oct 30 16:59:33 2014 +0000
Parent:
5:76845960c8a2
Commit message:
make the mbed become the ; * udp server to receive the data from the; * client.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed May 14 15:35:17 2014 +0000
+++ b/main.cpp	Thu Oct 30 16:59:33 2014 +0000
@@ -1,24 +1,35 @@
+/*****************************************
+*file:UDP Server app
+*Creator:JacobShi
+*Time:2014/10/29
+* Description: make the mbed become the 
+* udp server to receive the data from the
+* client.
+ ***************************************/
 #include "mbed.h"
 #include "EthernetInterface.h"
-
-#define ECHO_SERVER_PORT   7
-
-int main (void) {
+#define PORT 8080
+#define SIZE 100
+char data_buffer[100];
+int main(void)
+{
     EthernetInterface eth;
-    eth.init(); //Use DHCP
-    eth.connect();
-    printf("IP Address is %s\n", eth.getIPAddress());
-    
     UDPSocket server;
-    server.bind(ECHO_SERVER_PORT);
-    
-    Endpoint client;
-    char buffer[256];
-    while (true) {
-        printf("\nWait for packet...\n");
-        int n = server.receiveFrom(client, buffer, sizeof(buffer));
-        
-        printf("Received packet from: %s\n", client.get_address());
-        server.sendTo(client, buffer, n);
+    Endpoint endpoint;
+    eth.init();
+    eth.connect();
+    printf("the ipaddr of the mbed is%s\n",eth.getIPAddress());
+    server.bind(PORT);
+    printf("wait for the packeg\n");
+    while(1)
+    {
+       
+        int n=server.receiveFrom(endpoint,data_buffer,SIZE);
+        printf("the ipaddr of the client is :%s\n",endpoint.get_address());
+        data_buffer[n]='\0';
+        printf("%s\n", data_buffer);
+        server.sendTo(endpoint,"RecvOK",sizeof("RecvOK"));
+
     }
-}
+    return 0;
+}
\ No newline at end of file