From 7d8c54e29ec27897e70b14e016cee614f473b30e Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Sat, 8 Jan 2022 20:10:32 +0100 Subject: [PATCH] server.available() according to reference. with example --- examples/WiFiPagerServer/WiFiPagerServer.ino | 72 ++++++++++++++++++++ examples/WiFiPagerServer/arduino_secrets.h | 2 + src/WiFiServer.cpp | 6 +- 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100755 examples/WiFiPagerServer/WiFiPagerServer.ino create mode 100644 examples/WiFiPagerServer/arduino_secrets.h diff --git a/examples/WiFiPagerServer/WiFiPagerServer.ino b/examples/WiFiPagerServer/WiFiPagerServer.ino new file mode 100755 index 00000000..1f11cb5c --- /dev/null +++ b/examples/WiFiPagerServer/WiFiPagerServer.ino @@ -0,0 +1,72 @@ +/* + Pager Server + + The example is a simple server that echoes any incoming + messages to all connected clients. Connect two or more + telnet sessions to see how server.available() and + server.print() work. + + Circuit: + * WiFi 101 Shield attached + +*/ + +#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; + +WiFiServer server(23); + +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); + } + + server.begin(); + + IPAddress ip = WiFi.localIP(); + Serial.println(); + Serial.println("Connected to WiFi network."); + Serial.print("To access the server, connect with Telnet client to "); + Serial.print(ip); + Serial.println(" 23"); +} + +void loop() { + + WiFiClient client = server.available(); // returns first client which has data to read or a 'false' client + if (client) { // client is true only if it is connected and has data to read + String s = client.readStringUntil('\n'); // read the message incoming from one of the clients + s.trim(); // trim eventual \r + Serial.println(s); // print the message to Serial Monitor + client.print("echo: "); // this is only for the sending client + server.println(s); // send the message to all connected clients + server.flush(); // flush the buffers + } +} diff --git a/examples/WiFiPagerServer/arduino_secrets.h b/examples/WiFiPagerServer/arduino_secrets.h new file mode 100644 index 00000000..0c9fdd55 --- /dev/null +++ b/examples/WiFiPagerServer/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/src/WiFiServer.cpp b/src/WiFiServer.cpp index fa72ddca..469970ef 100644 --- a/src/WiFiServer.cpp +++ b/src/WiFiServer.cpp @@ -85,11 +85,7 @@ WiFiClient WiFiServer::available(uint8_t* status) } if (_socket != -1) { - SOCKET child = WiFiSocket.accepted(_socket); - - if (child > -1) { - return WiFiClient(child); - } + WiFiSocket.accepted(_socket); for (SOCKET s = 0; s < TCP_SOCK_MAX; s++) { if (WiFiSocket.hasParent(_socket, s) && WiFiSocket.available(s)) {