From 33902959db2c65bf30e5cda3e4d8e2fc47e0da82 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Fri, 22 Sep 2023 20:56:21 +0200 Subject: [PATCH] WiFiS3 WiFiServer::accept() --- .../WiFiAdvancedChatServer.ino | 104 ++++++++++++++++++ .../WiFiAdvancedChatServer/arduino_secrets.h | 2 + libraries/WiFiS3/src/WiFiServer.cpp | 17 ++- libraries/WiFiS3/src/WiFiServer.h | 1 + 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 libraries/WiFiS3/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino create mode 100644 libraries/WiFiS3/examples/WiFiAdvancedChatServer/arduino_secrets.h diff --git a/libraries/WiFiS3/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino b/libraries/WiFiS3/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino new file mode 100644 index 000000000..eb2e7cf66 --- /dev/null +++ b/libraries/WiFiS3/examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino @@ -0,0 +1,104 @@ +/* + 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. + + */ + +#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_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + String fv = WiFi.firmwareVersion(); + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { + Serial.println("Please upgrade the firmware"); + } + + // 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(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/libraries/WiFiS3/examples/WiFiAdvancedChatServer/arduino_secrets.h b/libraries/WiFiS3/examples/WiFiAdvancedChatServer/arduino_secrets.h new file mode 100644 index 000000000..0c9fdd556 --- /dev/null +++ b/libraries/WiFiS3/examples/WiFiAdvancedChatServer/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/libraries/WiFiS3/src/WiFiServer.cpp b/libraries/WiFiS3/src/WiFiServer.cpp index c2cbc9abc..c4429f9ba 100644 --- a/libraries/WiFiS3/src/WiFiServer.cpp +++ b/libraries/WiFiS3/src/WiFiServer.cpp @@ -32,6 +32,21 @@ WiFiClient WiFiServer::available() { return WiFiClient(); } +/* -------------------------------------------------------------------------- */ +WiFiClient WiFiServer::accept() { +/* -------------------------------------------------------------------------- */ + if(_sock != -1) { + string res = ""; + modem.begin(); + /* call the server accept on esp so that the accept is performed */ + if(modem.write(string(PROMPT(_SERVERACCEPT)),res, "%s%d\r\n" , CMD_WRITE(_SERVERACCEPT), _sock)) { + int client_sock = atoi(res.c_str()); + return WiFiClient(client_sock); + } + } + return WiFiClient(); +} + /* -------------------------------------------------------------------------- */ void WiFiServer::begin(int port) { /* -------------------------------------------------------------------------- */ @@ -87,4 +102,4 @@ void WiFiServer::end() { bool WiFiServer::operator==(const WiFiServer& whs) { return _sock == whs._sock; -} \ No newline at end of file +} diff --git a/libraries/WiFiS3/src/WiFiServer.h b/libraries/WiFiS3/src/WiFiServer.h index 44c5ee1ba..7359e510a 100644 --- a/libraries/WiFiS3/src/WiFiServer.h +++ b/libraries/WiFiS3/src/WiFiServer.h @@ -37,6 +37,7 @@ class WiFiServer : public Server { WiFiServer(); WiFiServer(int p); WiFiClient available(); + WiFiClient accept(); void begin(int port); void begin(); virtual size_t write(uint8_t);