TCP Echo Server for INDIA academy

Dependencies:   WIZnetInterface mbed

hercules download

Revision:
1:f8dd2f0193fe
Parent:
0:964e12c2946e
diff -r 964e12c2946e -r f8dd2f0193fe main.cpp
--- a/main.cpp	Wed Dec 06 07:48:41 2017 +0000
+++ b/main.cpp	Wed Dec 06 23:51:34 2017 +0000
@@ -17,6 +17,9 @@
 char echo_server_gateway[] = "192.168.0.1";
 int echo_server_port = 30000;
 
+//Close message
+char close_message[256] = "close";
+
 int main (void) 
 {
     printf("Wait a second...\r\n");
@@ -41,18 +44,24 @@
         char buffer[256];
         while (true) {
             int n = client.receive(buffer, sizeof(buffer));
-            if (n <= 0) break;
+            buffer[n] = '\0';
             
-            // print received message to terminal
-            buffer[n] = '\0';
-            printf("Received message from Client :'%s'\r\n",buffer);
+            if (strcmp(buffer,close_message)==0) {
+                printf("recv close message \r\n"); 
+                break;
+                }
+            else {
+                if (n > 0) {    
+                    // print received message to terminal
+                    printf("Received message from Client :'%s'\r\n",buffer);
                         
-            // print sending message to terminal
-            printf("Sending message to Client: '%s'\r\n",buffer);
+                    // print sending message to terminal
+                    printf("Sending message to Client: '%s'\r\n",buffer);
             
-            // Echo received message back to client
-            client.send_all(buffer, n);
-            if (n <= 0) break;
+                    // Echo received message back to client
+                    client.send_all(buffer, n);
+                }
+            }
         }
         
         client.close();