Initial Example Project

Dependencies:   SDFileSystem WIZnet_Library mbed

Fork of Nucleo_L152RE_W5500HelloWorld_ver2 by Edward ahn

Revision:
3:fa8925fb003a
Parent:
2:a2cf65b34267
Child:
4:17797698535b
--- a/main.cpp	Mon Jan 18 01:47:44 2016 +0000
+++ b/main.cpp	Mon Jan 18 10:16:16 2016 +0000
@@ -6,11 +6,9 @@
 #include "UDPSocket.h"
 
 //#define USE_DHCP    1 // DHCP 사용할때 사용
-//#define USE_UDP     1
-#define USE_TCP     1
 
-#define TCP_PORT    5000
-#define UDP_PORT    7000
+#define TCP_PORT        5000
+#define UDP_PORT        7000
 
 const char * IP_Addr    = "192.168.0.194";
 const char * IP_Subnet  = "255.255.255.0";
@@ -28,14 +26,14 @@
 WIZnetInterface ethernet(&spi, PB_6, PA_9);//scs(PB_6), nRESET(PA_9); // reset pin is dummy, don't affect any pin of WIZ550io
 
 int main() {
-
+        
     //Set serial port baudrate speed: 115200
     pc.baud(115200);
     wait(10);
     pc.printf("W5500 Application Started \r\n");
 
-    char buffer[256];
-    
+    char buffer[128];
+
     //SD-CARD
     printf("Hello World!\n");   
     mkdir("/sd/mydir", 0777);
@@ -45,7 +43,7 @@
     }
     fprintf(fp, "Hello fun SD Card World!");
     fclose(fp); 
-    printf("Goodbye World!\n");
+    printf("Goodbye World!\r\n");
     //SD-CARD END 
     
     while(1)
@@ -80,44 +78,44 @@
             pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
             exit(0);
         }
-#if USE_UDP
-        UDPSocket UDP_server;
+  
+        TCPSocketServer TCP_server; // TCP Socket open
+        UDPSocket UDP_server; // UDP Socket open
+
         UDP_server.init();
         Endpoint nist;
-        
         nist.set_address(UDP_IP, UDP_PORT);
         UDP_server.bind(UDP_PORT);
         pc.printf("UDP PORT %d \r\n", UDP_PORT);
-        pc.printf("\nWait for message...\r\n");
+
+        TCP_server.bind(TCP_PORT);
+        pc.printf("TCP PORT %d \r\n", TCP_PORT);
+        TCP_server.listen();
+
+        pc.printf("server %s on port %d: \r\n", nist.get_address(), nist.get_port());
+        int m = UDP_server.receiveFrom(nist, buffer, sizeof(buffer));
+            if(m > 0){
+            UDP_server.sendTo(nist, buffer, m);
+            pc.printf("UDP Data Send OK\r\n");                     
+        }
+        pc.printf("UDP Close\r\n");
         
         while (1) {
-            int n = UDP_server.receiveFrom(nist, buffer, sizeof(buffer));
-            if(n > 0){
-                UDP_server.sendTo(nist, buffer, n);
-            }
-        }
-#endif
-#if USE_TCP
-        TCPSocketServer server;
-        server.bind(TCP_PORT);
-        pc.printf("TCP PORT %d \r\n", TCP_PORT);
-        server.listen();
-        
-        while (1) {
-            pc.printf("\nWait for new connection...\r\n");
-            TCPSocketConnection client;
-            server.accept(client);
-            //client.set_blocking(false, 0); // Timeout=0.
-            pc.printf("Connection from: %s\r\n", client.get_address());
-            while (client.is_connected() == true) {
-                int n = client.receive(buffer, sizeof(buffer));
-                if(n > 0)
-                    client.send_all(buffer, n);
-                if(client.is_fin_received())
-                    client.close();
+            pc.printf("\nWait for TCP connection...\r\n");            
+            TCPSocketConnection TCP_client;
+            if(TCP_server.accept(TCP_client) == 0)
+            {
+                pc.printf("Connection from: %s\r\n", TCP_client.get_address());
+                while (TCP_client.is_connected() == true) {
+                   int n = TCP_client.receive(buffer, sizeof(buffer));
+                   if(n > 0)
+                       TCP_client.send_all(buffer, n);
+                   if(TCP_client.is_fin_received()){
+                       TCP_client.close();
+                    }
+                }
             }
             pc.printf("Disconnected.\r\n");
         }
-#endif
     }
 }