For lecture

Dependencies:   WIZnetInterface WIZwiki-W7500-lecture mbed

Fork of WIZwiki-W7500-lecture1-tcp by Bohyun Bang

Files at this revision

API Documentation at this revision

Comitter:
bangbh
Date:
Mon Mar 07 00:22:10 2016 +0000
Parent:
4:d164f370aa56
Commit message:
For lecture

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d164f370aa56 -r b0e5f03fe7b8 main.cpp
--- a/main.cpp	Mon Feb 01 23:27:54 2016 +0000
+++ b/main.cpp	Mon Mar 07 00:22:10 2016 +0000
@@ -6,29 +6,38 @@
     EthernetInterface eth;
     
     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0xFF, 0xFF};
+    char message[] = "Hello World!\r\n";
+    char buffer[400];
+    int ret;
+    
     eth.init(mac_addr, "192.168.0.180", "255.255.255.0", "192.168.0.1"); // USE Static IP
     //eth.init(mac_addr); //Use DHCP
     eth.connect();
      
     printf("IP Address is %s\r\n", eth.getIPAddress());
 
-    TCPSocketConnection tcp_sock;
-    tcp_sock.connect("192.168.0.244", 4545);
-    char message[] = "Hello World!\r\n";
-    tcp_sock.send(message, sizeof(message)-1);
-    char buffer[400];
-    int ret;
+    TCPSocketServer server;
+    server.bind(4545);
+    server.listen();
+    
     while (true)
     {
-        ret = tcp_sock.receive(buffer, sizeof(buffer)-1);
-        if (ret > 0)
-        {
-            buffer[ret] = '\0';
-            printf("Received %d chars from server: %s\n", ret, buffer);
+        TCPSocketConnection client;
+        server.accept(client);
+        client.set_blocking(false, 10000);//10 sec
+        
+        printf("Connection from: %s\r\n", client.get_address());
+        while(true){
+            int n = client.receive(buffer, sizeof(buffer));
+            if(n <= 0) break;
+            buffer[n] = '\0';
+            client.send(buffer,n);
+            if(n <= 0) break;
         }
+        printf("Disconnected\r\n");
+        client.close();
     }
-    
-    tcp_sock.close();
+    server.close();
     
     eth.disconnect();