From ae79a6d2111d4b6762506fd34900047f9365b395 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Sat, 8 Jan 2022 19:49:06 +0100 Subject: [PATCH] added server.accept() --- .../WiFiAdvancedChatServer.ino | 103 ++++++++++++++++++ .../WiFiAdvancedChatServer/arduino_secrets.h | 2 + keywords.txt | 1 + src/WiFiServer.cpp | 16 +++ src/WiFiServer.h | 1 + 5 files changed, 123 insertions(+) create mode 100644 examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino create mode 100644 examples/WiFiAdvancedChatServer/arduino_secrets.h diff --git a/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino b/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino new file mode 100644 index 00000000..ce7115bb --- /dev/null +++ b/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino @@ -0,0 +1,103 @@ +/* + Advanced WiFi Chat Server + + A more advanced server that distributes any incoming messages + to all connected clients but the client the message comes from. + To use, telnet to your device's IP address and type. + You can see the client's input in the serial monitor as well. + + Circuit: + * WiFi 101 Shield attached + +*/ + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + +int status = WL_IDLE_STATUS; + +// telnet defaults to port 23 +WiFiServer server(23); + +WiFiClient clients[8]; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi 101 Shield not present"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + // start the server: + server.begin(); + + Serial.print("Chat server address: "); + Serial.println(IPAddress(WiFi.localIP())); +} + +void loop() { + // check for any new client connecting, and say hello (before any incoming data) + WiFiClient newClient = server.accept(); + if (newClient) { + for (byte i=0; i < 8; i++) { + if (!clients[i]) { + Serial.print("We have a new client #"); + Serial.println(i); + newClient.print("Hello, client number: "); + newClient.println(i); + // Once we "accept", the client is no longer tracked by WiFiServer + // so we must store it into our list of clients + clients[i] = newClient; + break; + } + } + } + + // check for incoming data from all clients + for (byte i=0; i < 8; i++) { + if (clients[i] && clients[i].available() > 0) { + // read bytes from a client + byte buffer[80]; + int count = clients[i].read(buffer, 80); + // write the bytes to all other connected clients + for (byte j=0; j < 8; j++) { + if (j != i && clients[j].connected()) { + clients[j].write(buffer, count); + } + } + } + } + + // stop any clients which disconnect + for (byte i=0; i < 8; i++) { + if (clients[i] && !clients[i].connected()) { + Serial.print("disconnect client #"); + Serial.println(i); + clients[i].stop(); + } + } + +} diff --git a/examples/WiFiAdvancedChatServer/arduino_secrets.h b/examples/WiFiAdvancedChatServer/arduino_secrets.h new file mode 100644 index 00000000..0c9fdd55 --- /dev/null +++ b/examples/WiFiAdvancedChatServer/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/keywords.txt b/keywords.txt index 9757d07e..204cd0c9 100755 --- a/keywords.txt +++ b/keywords.txt @@ -21,6 +21,7 @@ connect KEYWORD2 connectSSL KEYWORD2 write KEYWORD2 available KEYWORD2 +accept KEYWORD2 read KEYWORD2 flush KEYWORD2 stop KEYWORD2 diff --git a/src/WiFiServer.cpp b/src/WiFiServer.cpp index fa72ddca..15d855d2 100644 --- a/src/WiFiServer.cpp +++ b/src/WiFiServer.cpp @@ -101,6 +101,22 @@ WiFiClient WiFiServer::available(uint8_t* status) return WiFiClient(); } +WiFiClient WiFiServer::accept() +{ + if (_socket != -1 && !WiFiSocket.listening(_socket)) { + _socket = -1; + } + + if (_socket != -1) { + SOCKET child = WiFiSocket.accepted(_socket); + + if (child > -1) { + return WiFiClient(child); + } + } + return WiFiClient(); +} + uint8_t WiFiServer::status() { // Deprecated. return 0; diff --git a/src/WiFiServer.h b/src/WiFiServer.h index 22ff743d..13761440 100644 --- a/src/WiFiServer.h +++ b/src/WiFiServer.h @@ -35,6 +35,7 @@ class WiFiServer : public Server { public: WiFiServer(uint16_t); WiFiClient available(uint8_t* status = NULL); + WiFiClient accept(); void begin(); uint8_t beginSSL(); virtual size_t write(uint8_t);