Simple TCP/IP Server using the UIPEthernet library for ENC28J60 Ethernet boards.

Dependencies:   UIPEthernet

Revision:
3:3a4fc577ecaa
Parent:
2:60ca1737aa47
Child:
4:f7aed299f625
--- a/main.cpp	Fri Jun 30 20:16:19 2017 +0000
+++ b/main.cpp	Fri Aug 30 19:18:40 2019 +0000
@@ -2,71 +2,74 @@
  * Simple TcpServer using the UIPEthernet library for ENC28J60 Ethernet boards.
  *
  */
+#include "mbed.h"
+#include "UipEthernet.h"
+#include "TcpServer.h"
+#include "TcpClient.h"
 
-#include "mbed.h"
-#include "UIPEthernet.h"
-#include "UIPServer.h"
-#include "UIPClient.h"
-
-Serial  pc(USBTX, USBRX);
+#define IP      "192.168.1.35"
+#define GATEWAY "192.168.1.1"
+#define NETMASK "255.255.255.0"
+#define PORT    10001
 
-#if defined(TARGET_LPC1768)
-UIPEthernet    uIPEthernet(p11, p12, p13, p8);         // mosi, miso, sck, cs
-#elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8)  \
-   || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8)  \
-   || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB)  \
-   || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB)  \
-   || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
-   || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
-   || defined(TARGET_NRF51822) \
-   || defined(TARGET_RZ_A1H)
-UIPEthernet    uIPEthernet(D11, D12, D13, D10);        // mosi, miso, sck, cs
-#endif
+const uint8_t   MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
+UipEthernet     net(MAC, D11, D12, D13, D10);   // mac, mosi, miso, sck, cs
+TcpServer       server;                         // Ethernet server
+TcpClient*      client;
+uint8_t         recvData[1024];
+const char      sendData[] = "Data received OK";
 
-// MAC address must be unique within the connected network. Modify as appropriate.
-const uint8_t   MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
-const uint16_t  MY_PORT = 10001;
-EthernetServer  server = EthernetServer(MY_PORT);
-
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
 int main(void)
 {
-//#define DHCP    1   // if you'd like to use static IP address comment out this line 
-#if defined(DHCP)
-    pc.printf("Searching for DHCP server..\r\n");
-    if(uIPEthernet.begin(MY_MAC) != 1) {
-        pc.printf("No DHCP server found.\r\n");
-        pc.printf("Exiting application.\r\n");
-        return 0;
-    }
-    pc.printf("DHCP server found.\r\n");
-#else
-    // IP address must be unique and compatible with your network.
-    const IPAddress MY_IP(192, 168, 1, 33);    //  Change as appropriate.
-    uIPEthernet.begin(MY_MAC, MY_IP);
-#endif
-    IPAddress   localIP = uIPEthernet.localIP();
-    pc.printf("Local IP = %s\r\n", localIP.toString());
-    server.begin();
+    printf("Starting ...\r\n");
+
+    //net.set_network(IP, NETMASK, GATEWAY);  // include this to use static IP address
+    net.connect();
+
+    // Show the network address
+    const char*     ip = net.get_ip_address();
+    const char*     netmask = net.get_netmask();
+    const char*     gateway = net.get_gateway();
+
+    printf("IP address: %s\r\n", ip ? ip : "None");
+    printf("Netmask: %s\r\n", netmask ? netmask : "None");
+    printf("Gateway: %s\r\n\r\n", gateway ? gateway : "None");
+
+    /* Open the server on ethernet stack */
+    server.open(&net);
+
+    /* Bind the HTTP port (TCP 80) to the server */
+    server.bind(PORT);
 
-    while(1) {
-        size_t size;
+    /* Can handle 5 simultaneous connections */
+    server.listen(5);
+
+    while (true) {
+        client = server.accept();
+
+        if (client) {
+            size_t  recvLen;
 
-        if (EthernetClient client = server.available()) {
-            pc.printf("Client connected.\r\n");
-            pc.printf("Data received from client:\r\n");
-            while((size = client.available()) > 0) {
-                uint8_t* msg = (uint8_t*)malloc(size);
-                size = client.read(msg, size);
-                for(int i = 0; i < size; i++)
-                    pc.printf("0x%x ", msg[i]);
-                pc.printf("\r\n");
-                free(msg);
-                char* acknowledge = "Data received OK";
-                client.write((uint8_t*)acknowledge, strlen(acknowledge));
-                pc.printf(acknowledge);
+            printf("\r\n----------------------------------\r\n");
+            printf("Client with IP address %s connected.\n\r", client->getpeername());
+            if ((recvLen = client->available()) > 0) {
+                printf("%d bytes received:\r\n", recvLen);
+                client->recv(recvData, recvLen);
+                for (int i = 0; i < recvLen; i++)
+                    printf(" 0x%.2X", recvData[i]);
+                printf("\r\n");
+                client->send((uint8_t*)sendData, strlen(sendData));
+                printf("%s\r\n", sendData);
             }
-            client.stop();
-            pc.printf("Client disconnected.\r\n");
+
+            printf("Client with IP address %s disconnected.\r\n", client->getpeername());
+            client->close();
         }
     }
-}
\ No newline at end of file
+}