UIPEthernet library for Arduino IDE, Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32,Nordic nRF51,Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network chip. Compatible with Wiznet W5100 Ethernet library API. Compiled and tested on Nucleo-F302R8. Master repository is: https://github.com/UIPEthernet/UIPEthernet/

Revision:
32:e77cbe3783e5
Parent:
11:3fb19220d9ec
--- a/examples/AdvancedChatServer/AdvancedChatServer.ino	Tue Feb 07 14:18:10 2017 +0100
+++ b/examples/AdvancedChatServer/AdvancedChatServer.ino	Fri Feb 10 09:10:47 2017 +0100
@@ -20,6 +20,18 @@
 
  */
 
+// Enter a MAC address and IP address for your controller below.
+// The IP address will be dependent on your local network.
+
+#define MACADDRESS 0x00,0x01,0x02,0x03,0x04,0x05
+#define MYIPADDR 192,168,1,6
+#define MYIPMASK 255,255,255,0
+#define MYDNS 192,168,1,1
+#define MYGW 192,168,1,1
+// telnet defaults to port 23
+#define LISTENPORT 23
+#define UARTBAUD 115200
+
 #if defined(__MBED__)
   #include <mbed.h>
   #include "mbed/millis.h"
@@ -31,14 +43,13 @@
 #include <UIPEthernet.h>
 #include <utility/logging.h>
 
-// Enter a MAC address and IP address for your controller below.
-// The IP address will be dependent on your local network.
+  uint8_t mac[6] = {MACADDRESS};
+  uint8_t myIP[4] = {MYIPADDR};
+  uint8_t myMASK[4] = {MYIPMASK};
+  uint8_t myDNS[4] = {MYDNS};
+  uint8_t myGW[4] = {MYGW};
 
-uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-IPAddress ip(192,168,0,6);
-
-// telnet defaults to port 23
-EthernetServer server(23);
+EthernetServer server(LISTENPORT);
 
 EthernetClient clients[4];
 
@@ -49,17 +60,19 @@
 int main() {
 #endif  
   // initialize the ethernet device
-  Ethernet.begin(mac, ip);
+  //Ethernet.begin(mac,myIP);
+  Ethernet.begin(mac,myIP,myDNS,myGW,myMASK);
   // start listening for clients
   server.begin();
  // Open serial communications and wait for port to open:
   #if ACTLOGLEVEL>LOG_NONE
 
     #if defined(ARDUINO)
-      LogObject.begin(9600);
+      LogObject.begin(UARTBAUD);
     #endif
     #if defined(__MBED__)
       Serial LogObject(SERIAL_TX,SERIAL_RX);
+      LogObject.baud(UARTBAUD);
     #endif
     while (!LogObject)
       {
@@ -68,12 +81,20 @@
   #endif
 
   #if ACTLOGLEVEL>=LOG_INFO
-    LogObject.uart_send_str(F("Chat server address:"));
+    LogObject.uart_send_str(F("Chat server listen on:"));
     #if defined(ARDUINO)
-      LogObject.println(Ethernet.localIP());
+      LogObject.print(Ethernet.localIP()[0]);
+      LogObject.print(F("."));
+      LogObject.print(Ethernet.localIP()[1]);
+      LogObject.print(F("."));
+      LogObject.print(Ethernet.localIP()[2]);
+      LogObject.print(F("."));
+      LogObject.print(Ethernet.localIP()[3]);
+      LogObject.print(F(":"));
+      LogObject.println(LISTENPORT);
     #endif  
     #if defined(__MBED__)
-      LogObject.printf("%d.%d.%d.%d",Ethernet.localIP()[0],Ethernet.localIP()[1],Ethernet.localIP()[2],Ethernet.localIP()[3]);
+      LogObject.printf("%d.%d.%d.%d:%d\r\n",Ethernet.localIP()[0],Ethernet.localIP()[1],Ethernet.localIP()[2],Ethernet.localIP()[3],LISTENPORT);
     #endif  
   #endif
 
@@ -89,46 +110,72 @@
   // wait for a new client:
   EthernetClient client = server.available();
 
-  if (client) {
-
+  if (client)
+    {
+    //check whether this client refers to the same socket as one of the existing instances:
     bool newClient = true;
-    for (uint8_t i=0;i<4;i++) {
-      //check whether this client refers to the same socket as one of the existing instances:
-      if (clients[i]==client) {
-        newClient = false;
-        break;
-      }
-    }
+    uint8_t i=0;
+    while ((i<4) && (clients[i]!=client))
+      {i++;}
+    if (i<4) {newClient = false;}
 
-    if (newClient) {
-      //check which of the existing clients can be overridden:
-      for (uint8_t i=0;i<4;i++) {
-        if (!clients[i] && clients[i]!=client) {
-          clients[i] = client;
-          // clead out the input buffer:
-          client.flush();
-          // clead out the input buffer:
-          client.flush();
-          #if ACTLOGLEVEL>=LOG_INFO
-            LogObject.uart_send_strln(F("We have a new client"));
-          #endif
-            client.println(F("Hello, client!"));
-            client.print(F("my IP: "));
-            client.println(Ethernet.localIP());
-          break;
+    if (newClient)
+      {
+      #if ACTLOGLEVEL>=LOG_INFO
+        LogObject.uart_send_strln(F("New client try connect"));
+      #endif
+      //Search unused client:
+      uint8_t j=0;
+      while ((j<4) && (clients[j]))
+        {j++;}
+      if (j>=4)
+        {
+        #if ACTLOGLEVEL>=LOG_INFO
+          LogObject.uart_send_strln(F("Too many client"));
+        #endif
+        client.stop();
+        }
+      else
+        {
+        #if ACTLOGLEVEL>=LOG_INFO
+          LogObject.uart_send_str(F("Save client to ID:"));
+          LogObject.uart_send_decln(j);
+        #endif
+        clients[j] = client;
+        // clead out the input buffer:
+        client.flush();
+        #if ACTLOGLEVEL>=LOG_INFO
+          LogObject.uart_send_strln(F("We have a new client"));
+        #endif
+        client.println(F("Hello, client!"));
+        client.print(F("my IP: "));
+        client.println(Ethernet.localIP());
         }
       }
-    }
 
-    if (client.available() > 0) {
+    if (client.available() > 0)
+      {
+      i=0;
+      while ((i<4) && (clients[i]!=client))
+        {i++;}
+      #if ACTLOGLEVEL>=LOG_INFO
+        LogObject.uart_send_str(F("Message received from client ID:"));
+        LogObject.uart_send_decln(i);
+      #endif
       // read the bytes incoming from the client:
       char thisChar = client.read();
       // echo the bytes back to all other connected clients:
-      for (uint8_t i=0;i<4;i++) {
-        if (clients[i] && clients[i]!=client) {
-          clients[i].write(thisChar);
+      for (uint8_t j=0;j<4;j++)
+        {
+        if (clients[j] && clients[j]!=client)
+          {
+          #if ACTLOGLEVEL>=LOG_INFO
+            LogObject.uart_send_str(F("Message forwarded to client ID:"));
+            LogObject.uart_send_decln(j);
+          #endif
+          clients[j].write(thisChar);
+          }
         }
-      }
       // echo the bytes to the server as well:
       #if ACTLOGLEVEL>=LOG_INFO
         #if defined(ARDUINO)