Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkClient - close the connection in stop() method #9542

Merged
merged 3 commits into from
May 10, 2024

Conversation

JAndrassy
Copy link
Contributor

@JAndrassy JAndrassy commented Apr 20, 2024

this PR allows to close the TCP connection in client.stop() for all copies of NetworkClient which wrap the same TCP connection.

The existence of multiple copies of WiFiClient for the same TCP connection is a feature of the 'Arduino language' and the networking libraries must deal with it.

Destroy of a NetworkClient instance should not close the TCP connection unless it is the last copy handling that connection. Thanks to shared_ptr NetworkClientSocketHandle instance closes the connection as it is destroyed when the last copy of NetworkClient for that connection is destroyed.

tested with WiFiPagerServer example from my NetAPIHelpers library with this added:

    if (s.equals("C")) {
      client.stop();
    }

Copy link
Contributor

github-actions bot commented Apr 20, 2024

Messages
📖 You might consider squashing your 3 commits (simplifying branch history).

👋 Hello JAndrassy, we appreciate your contribution to this project!


Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Addressing info messages (📖) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against efa7630

Copy link
Contributor

github-actions bot commented Apr 20, 2024

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP32S30⚠️ +1400.00⚠️ +0.03000.000.00
ESP32S20⚠️ +1520.00⚠️ +0.03000.000.00
ESP32C30⚠️ +760.00⚠️ +0.01000.000.00
ESP32C60⚠️ +760.00⚠️ +0.02000.000.00
ESP32H20⚠️ +680.00⚠️ +0.01000.000.00
ESP320⚠️ +1480.00⚠️ +0.03000.000.00
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32S3ESP32S2ESP32C3ESP32C6ESP32H2ESP32
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
ArduinoOTA/examples/BasicOTA⚠️ +1360⚠️ +1440⚠️ +720⚠️ +700--⚠️ +1360
AsyncUDP/examples/AsyncUDPClient⚠️ +560⚠️ +5600000--⚠️ +560
AsyncUDP/examples/AsyncUDPMulticastServer⚠️ +560⚠️ +5600000--⚠️ +560
AsyncUDP/examples/AsyncUDPServer⚠️ +560⚠️ +6400000--⚠️ +560
BLE/examples/BLE5_extended_scan00--000000--
BLE/examples/BLE5_multi_advertising00--000000--
BLE/examples/BLE5_periodic_advertising00--000000--
BLE/examples/BLE5_periodic_sync00--000000--
BLE/examples/Beacon_Scanner00--00000000
BLE/examples/Client00--00000000
BLE/examples/EddystoneTLM_Beacon00--0000--00
BLE/examples/EddystoneURL_Beacon00--0000--00
BLE/examples/Notify00--00000000
BLE/examples/Scan00--00000000
BLE/examples/Server00--00000000
BLE/examples/Server_multiconnect00--00000000
BLE/examples/UART00--00000000
BLE/examples/Write00--00000000
BLE/examples/iBeacon00--00000000
DNSServer/examples/CaptivePortal⚠️ +1320⚠️ +1360⚠️ +700⚠️ +700--⚠️ +1480
EEPROM/examples/eeprom_class000000000000
EEPROM/examples/eeprom_extra000000000000
EEPROM/examples/eeprom_write000000000000
ESP32/examples/AnalogOut/LEDCFade000000000000
ESP32/examples/AnalogOut/LEDCSoftwareFade000000000000
ESP32/examples/AnalogOut/SigmaDelta000000000000
ESP32/examples/AnalogOut/ledcFrequency000000000000
ESP32/examples/AnalogOut/ledcWrite_RGB000000000000
ESP32/examples/AnalogRead000000000000
ESP32/examples/AnalogReadContinuous000000000000
ESP32/examples/ArduinoStackSize000000000000
ESP32/examples/CI/CIBoardsTest000000000000
ESP32/examples/Camera/CameraWebServer⚠️ +560⚠️ +560------⚠️ +560
ESP32/examples/ChipID/GetChipID000000000000
ESP32/examples/DeepSleep/ExternalWakeUp0000------00
ESP32/examples/DeepSleep/TimerWakeUp00000000--00
ESP32/examples/DeepSleep/TouchWakeUp0000------00
ESP32/examples/FreeRTOS/BasicMultiThreading000000000000
ESP32/examples/FreeRTOS/Mutex000000000000
ESP32/examples/FreeRTOS/Queue000000000000
ESP32/examples/FreeRTOS/Semaphore000000000000
ESP32/examples/GPIO/BlinkRGB000000000000
ESP32/examples/GPIO/FunctionalInterrupt000000000000
ESP32/examples/GPIO/FunctionalInterruptStruct000000000000
ESP32/examples/GPIO/GPIOInterrupt000000000000
ESP32/examples/HWCDC_Events00--000000--
ESP32/examples/MacAddress/GetMacAddress000000000000
ESP32/examples/RMT/RMTCallback000000000000
ESP32/examples/RMT/RMTLoopback000000000000
ESP32/examples/RMT/RMTReadXJT000000000000
ESP32/examples/RMT/RMTWriteNeoPixel000000000000
ESP32/examples/RMT/RMT_CPUFreq_Test000000000000
ESP32/examples/RMT/RMT_EndOfTransmissionState000000000000
ESP32/examples/RMT/RMT_LED_Blink000000000000
ESP32/examples/ResetReason/ResetReason000000000000
ESP32/examples/ResetReason/ResetReason2000000000000
ESP32/examples/Serial/BaudRateDetect_Demo000000000000
ESP32/examples/Serial/OnReceiveError_BREAK_Demo000000000000
ESP32/examples/Serial/OnReceive_Demo000000000000
ESP32/examples/Serial/RS485_Echo_Demo000000000000
ESP32/examples/Serial/RxFIFOFull_Demo000000000000
ESP32/examples/Serial/RxTimeout_Demo000000000000
ESP32/examples/Serial/Serial_All_CPU_Freqs000000000000
ESP32/examples/Serial/Serial_STD_Func_OnReceive000000000000
ESP32/examples/Serial/onReceiveExample000000000000
ESP32/examples/TWAI/TWAIreceive000000000000
ESP32/examples/TWAI/TWAItransmit000000000000
ESP32/examples/Template/ExampleTemplate000000000000
ESP32/examples/Time/SimpleTime⚠️ +560⚠️ +5600000--⚠️ +560
ESP32/examples/Timer/RepeatTimer000000000000
ESP32/examples/Timer/WatchdogTimer000000000000
ESP32/examples/Touch/TouchButtonV20000--------
ESP32/examples/Touch/TouchInterrupt0000------00
ESP32/examples/Touch/TouchRead0000------00
ESP32/examples/Utilities/HEXBuilder000000000000
ESP32/examples/Utilities/MD5Builder000000000000
ESP32/examples/Utilities/SHA1Builder000000000000
ESP_I2S/examples/ES8388_loopback000000000000
ESP_I2S/examples/Record_to_WAV00--------00
ESP_I2S/examples/Simple_tone000000000000
ESP_NOW/examples/ESP_NOW_Broadcast_Master⚠️ +560⚠️ +5600000--⚠️ +480
ESP_NOW/examples/ESP_NOW_Broadcast_Slave⚠️ +560⚠️ +5600000--⚠️ +560
ESP_NOW/examples/ESP_NOW_Network⚠️ +560⚠️ +5600000--⚠️ +560
ESP_NOW/examples/ESP_NOW_Serial⚠️ +560⚠️ +5600000--⚠️ +560
ESP_SR/examples/Basic00----------
ESPmDNS/examples/mDNS-SD_Extended⚠️ +560⚠️ +5600000--⚠️ +560
ESPmDNS/examples/mDNS_Web_Server⚠️ +1240⚠️ +1400⚠️ +740⚠️ +740--⚠️ +1400
Ethernet/examples/ETH_W5500_Arduino_SPI⚠️ +1360⚠️ +1360⚠️ +680⚠️ +680⚠️ +680⚠️ +1400
Ethernet/examples/ETH_W5500_IDF_SPI⚠️ +1360⚠️ +1160⚠️ +680⚠️ +680⚠️ +680⚠️ +1400
FFat/examples/FFat_Test000000000000
FFat/examples/FFat_time⚠️ +560⚠️ +5600000--⚠️ +560
HTTPClient/examples/Authorization⚠️ +1280⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1440
HTTPClient/examples/BasicHttpClient⚠️ +1280⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1440
HTTPClient/examples/BasicHttpsClient⚠️ +1120⚠️ +1480⚠️ +720⚠️ +720--⚠️ +1480
HTTPClient/examples/HTTPClientEnterprise⚠️ +1320⚠️ +1360⚠️ +700⚠️ +700--⚠️ +1240
HTTPClient/examples/ReuseConnection⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1280
HTTPClient/examples/StreamHttpClient⚠️ +1240⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1480
HTTPUpdate/examples/httpUpdate⚠️ +1320⚠️ +1120⚠️ +740⚠️ +720--⚠️ +1400
HTTPUpdate/examples/httpUpdateSPIFFS⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1440
HTTPUpdate/examples/httpUpdateSecure⚠️ +1320⚠️ +1280⚠️ +740⚠️ +720--⚠️ +1400
HTTPUpdateServer/examples/WebUpdater⚠️ +1320⚠️ +1480⚠️ +720⚠️ +720--⚠️ +1400
Insights/examples/DiagnosticsSmokeTest⚠️ +560⚠️ +56000----⚠️ +560
Insights/examples/MinimalDiagnostics⚠️ +560⚠️ +56000----⚠️ +560
LittleFS/examples/LITTLEFS_test000000000000
LittleFS/examples/LITTLEFS_time⚠️ +560⚠️ +5600000--⚠️ +560
NetBIOS/examples/ESP_NBNST⚠️ +560⚠️ +5600000--⚠️ +560
NetworkClientSecure/examples/WiFiClientInsecure⚠️ +1320⚠️ +1480⚠️ +680⚠️ +680--⚠️ +1440
NetworkClientSecure/examples/WiFiClientPSK⚠️ +1360⚠️ +1360⚠️ +700⚠️ +680--⚠️ +1480
NetworkClientSecure/examples/WiFiClientSecure⚠️ +1320⚠️ +1440⚠️ +700⚠️ +700--⚠️ +1400
NetworkClientSecure/examples/WiFiClientSecureEnterprise⚠️ +1360⚠️ +1120⚠️ +700⚠️ +700--⚠️ +1400
NetworkClientSecure/examples/WiFiClientSecureProtocolUpgrade⚠️ +1320⚠️ +1400⚠️ +700⚠️ +700--⚠️ +1400
NetworkClientSecure/examples/WiFiClientShowPeerCredentials⚠️ +1360⚠️ +1360⚠️ +700⚠️ +720--⚠️ +1400
NetworkClientSecure/examples/WiFiClientTrustOnFirstUse⚠️ +1320⚠️ +1360⚠️ +720⚠️ +700--⚠️ +1400
PPP/examples/PPP_Basic⚠️ +1360⚠️ +1360⚠️ +680⚠️ +680⚠️ +680⚠️ +1400
Preferences/examples/Prefs2Struct000000000000
Preferences/examples/StartCounter000000000000
RainMaker/examples/RMakerCustom⚠️ +560⚠️ +56000----⚠️ +560
RainMaker/examples/RMakerCustomAirCooler⚠️ +560⚠️ +56000----⚠️ +560
RainMaker/examples/RMakerSonoffDualR3⚠️ +560⚠️ +56000----⚠️ +560
RainMaker/examples/RMakerSwitch⚠️ +560⚠️ +56000----⚠️ +560
SD/examples/SD_Test000000000000
SD/examples/SD_time⚠️ +560⚠️ +5600000--⚠️ +560
SD_MMC/examples/SDMMC_Test00--------00
SD_MMC/examples/SDMMC_time⚠️ +560--------⚠️ +560
SPI/examples/SPI_Multiple_Buses0000------00
SPIFFS/examples/SPIFFS_Test000000000000
SPIFFS/examples/SPIFFS_time⚠️ +560⚠️ +5600000--⚠️ +560
SimpleBLE/examples/SimpleBleDevice00----000000
TFLiteMicro/examples/hello_world000000000000
TFLiteMicro/examples/micro_speech000000000000
Ticker/examples/Blinker000000000000
Ticker/examples/TickerBasic000000000000
Ticker/examples/TickerParameter000000000000
USB/examples/CompositeDevice0000--------
USB/examples/ConsumerControl0000--------
USB/examples/CustomHIDDevice0000--------
USB/examples/FirmwareMSC0000--------
USB/examples/Gamepad0000--------
USB/examples/HIDVendor0000--------
USB/examples/Keyboard/KeyboardLogout0000--------
USB/examples/Keyboard/KeyboardMessage0000--------
USB/examples/Keyboard/KeyboardReprogram0000--------
USB/examples/Keyboard/KeyboardSerial0000--------
USB/examples/KeyboardAndMouseControl0000--------
USB/examples/MIDI/MidiController0000--------
USB/examples/MIDI/MidiInterface0000--------
USB/examples/MIDI/MidiMusicBox0000--------
USB/examples/MIDI/ReceiveMidi0000--------
USB/examples/Mouse/ButtonMouseControl0000--------
USB/examples/SystemControl0000--------
USB/examples/USBMSC0000--------
USB/examples/USBSerial0000--------
USB/examples/USBVendor0000--------
Update/examples/AWS_S3_OTA_Update⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1400
Update/examples/HTTPS_OTA_Update⚠️ +560⚠️ +5600000--⚠️ +560
Update/examples/HTTP_Client_AES_OTA_Update⚠️ +1320⚠️ +1440⚠️ +720⚠️ +720--⚠️ +1400
Update/examples/HTTP_Server_AES_OTA_Update⚠️ +1320⚠️ +1480⚠️ +720⚠️ +740--⚠️ +1400
Update/examples/OTAWebUpdater⚠️ +1240⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1440
Update/examples/SD_Update000000000000
WebServer/examples/AdvancedWebServer⚠️ +1320⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1400
WebServer/examples/FSBrowser⚠️ +1360⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1280
WebServer/examples/HelloServer⚠️ +1320⚠️ +1400⚠️ +740⚠️ +740--⚠️ +1280
WebServer/examples/HttpAdvancedAuth⚠️ +1160⚠️ +1360⚠️ +700⚠️ +700--⚠️ +1400
WebServer/examples/HttpAuthCallback⚠️ +1360⚠️ +1400⚠️ +700⚠️ +700--⚠️ +1440
WebServer/examples/HttpAuthCallbackInline⚠️ +1160⚠️ +1360⚠️ +700⚠️ +700--⚠️ +1400
WebServer/examples/HttpBasicAuth⚠️ +1320⚠️ +1360⚠️ +700⚠️ +700--⚠️ +1400
WebServer/examples/HttpBasicAuthSHA1⚠️ +1360⚠️ +1360⚠️ +720⚠️ +740--⚠️ +1400
WebServer/examples/HttpBasicAuthSHA1orBearerToken⚠️ +1320⚠️ +1160⚠️ +740⚠️ +740--⚠️ +1440
WebServer/examples/MultiHomedServers⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1240
WebServer/examples/PathArgServer⚠️ +1320⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1400
WebServer/examples/SDWebServer⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1440
WebServer/examples/SimpleAuthentification⚠️ +1320⚠️ +1360⚠️ +760⚠️ +760--⚠️ +1400
WebServer/examples/UploadHugeFile⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1440
WebServer/examples/WebServer⚠️ +1320⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1440
WebServer/examples/WebUpdate⚠️ +1360⚠️ +1320⚠️ +700⚠️ +700--⚠️ +1400
WiFi/examples/FTM/FTM_Initiator⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/FTM/FTM_Responder⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/SimpleWiFiServer⚠️ +1360⚠️ +1520⚠️ +700⚠️ +700--⚠️ +1400
WiFi/examples/WPS⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiAccessPoint⚠️ +1160⚠️ +1360⚠️ +700⚠️ +700--⚠️ +1400
WiFi/examples/WiFiBlueToothSwitch⚠️ +560--0000--⚠️ +560
WiFi/examples/WiFiClient⚠️ +1280⚠️ +1480⚠️ +700⚠️ +700--⚠️ +1400
WiFi/examples/WiFiClientBasic⚠️ +1280⚠️ +1160⚠️ +720⚠️ +720--⚠️ +1440
WiFi/examples/WiFiClientConnect⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiClientEnterprise⚠️ +1320⚠️ +1360⚠️ +720⚠️ +700--⚠️ +1400
WiFi/examples/WiFiClientEvents⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiClientStaticIP⚠️ +1120⚠️ +1320⚠️ +740⚠️ +740--⚠️ +1400
WiFi/examples/WiFiIPv6⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiMulti⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiMultiAdvanced⚠️ +1400⚠️ +1400⚠️ +720⚠️ +720--⚠️ +1400
WiFi/examples/WiFiScan⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiScanAsync⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiScanDualAntenna⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiSmartConfig⚠️ +560⚠️ +5600000--⚠️ +560
WiFi/examples/WiFiTelnetToSerial⚠️ +1320⚠️ +1360⚠️ +720⚠️ +720--⚠️ +1440
WiFi/examples/WiFiUDPClient⚠️ +560⚠️ +5600000--⚠️ +560
WiFiProv/examples/WiFiProv⚠️ +560⚠️ +5600000--⚠️ +560
Wire/examples/WireMaster000000000000
Wire/examples/WireScan000000000000
Wire/examples/WireSlave000000000000
BluetoothSerial/examples/DiscoverConnect----------00
BluetoothSerial/examples/GetLocalMAC----------00
BluetoothSerial/examples/SerialToSerialBT----------00
BluetoothSerial/examples/SerialToSerialBTM----------00
BluetoothSerial/examples/SerialToSerialBT_Legacy----------00
BluetoothSerial/examples/SerialToSerialBT_SSP----------00
BluetoothSerial/examples/bt_classic_device_discovery----------00
BluetoothSerial/examples/bt_remove_paired_devices----------00
ESP32/examples/DeepSleep/SmoothBlink_ULP_Code----------00
ESP32/examples/Touch/TouchButton----------00
Ethernet/examples/ETH_LAN8720----------⚠️ +1400
Ethernet/examples/ETH_TLK110----------⚠️ +1400

@VojtechBartoska
Copy link
Collaborator

Thanks for PR @JAndrassy, we will review it soon.

@VojtechBartoska VojtechBartoska added the Status: Review needed Issue or PR is awaiting review label Apr 23, 2024
@me-no-dev
Copy link
Member

@JAndrassy can you come up with a test case for this? Think of a sketch that would use multiple copies of the client and close when it should. The sketch should not require external libs

@JAndrassy
Copy link
Contributor Author

JAndrassy commented Apr 29, 2024

the best example is an attempt to implement server.available() and print-to-all-clients in 'external library'.
here is a simple version of the class put directly into the sketch. the rest of the sketch is my PagerServer example which is available in multiple Arduino networking libraries.
without this PR it is not possible to stop the client from the sketch.
send upper case C from telnet to invoke client.stop from server side.
I use ArduinoWiFiServer in my popular TelnetStream library and users don't like that they can't stop the client from server side on esp32. JAndrassy/TelnetStream#23

#include <WiFi.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

#define MAX_MONITORED_CLIENTS 5

class ArduinoWiFiServer : public WiFiServer, public Print {
public:

  ArduinoWiFiServer(uint16_t port) : WiFiServer(port) {}

  // https://www.arduino.cc/en/Reference/WiFiServerAvailable
  WiFiClient available() {

    // update connected clients
    for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
      if (!connectedClients[i]) {
        connectedClients[i] = accept();
      }
    }
    // find next client with data available
    for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
      if (index == MAX_MONITORED_CLIENTS) {
        index = 0;
      }
      WiFiClient& client = connectedClients[index];
      index++;
      if (client.available())
        return client;
    }
    return WiFiClient(); // no client with data found
  }

  virtual size_t write(uint8_t b) override {
    return write(&b, 1);
  }

  virtual size_t write(const uint8_t *buf, size_t size) override {
    size_t ret = 0;
    for (uint8_t i = 0; i < MAX_MONITORED_CLIENTS; i++) {
      WiFiClient& client = connectedClients[i];
      if (client) {
        ret = client.write(buf, size);
      }
    }
    return ret;
  }

  using Print::write;

private:
  WiFiClient connectedClients[MAX_MONITORED_CLIENTS];
  uint8_t index = 0;
};

ArduinoWiFiServer server(2323);

void setup() {

  Serial.begin(115200);
  while (!Serial);

  Serial.print("Attempting to connect to SSID \"");
  Serial.print(ssid);
  Serial.println("\" with DHCP ...");
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println();

  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(" 2323");
}

void loop() {

  WiFiClient client = server.available();     // returns first client which has data to read or a 'false' client
  if (client) {                               // client is here 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
    if (s == "C") {
      client.println("Bye");
      client.stop();                          // stop the client
    } else {
      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
    }
  }
}

@VojtechBartoska
Copy link
Collaborator

Status update:

@JAndrassy
Copy link
Contributor Author

the sketch above is an almost real world example. of course for a CI test a much simpler example would work

@JAndrassy
Copy link
Contributor Author

a simple test:

  if (client.connect(server, 80)) {
    Serial.println("connected to server");

    WiFiClient client2 = client;
    client2.stop();
    delay(3000);
    if (client.connected()) {
      Serial.println("still connected");
    }
  }

@P-R-O-C-H-Y
Copy link
Member

a simple test:

  if (client.connect(server, 80)) {
    Serial.println("connected to server");

    WiFiClient client2 = client;
    client2.stop();
    delay(3000);
    if (client.connected()) {
      Serial.println("still connected");
    }
  }

Hello @JAndrassy, can you please provide full small example which fails without your changes and work afterwards?
I can't make it work with the changes from your PR with the simple test you commented. Thanks

@JAndrassy
Copy link
Contributor Author

#include <WiFi.h>

const char* ssid = "yourssid";
const char* password = "yourpasswd";

const char* server = "arduino.tips";

WiFiClient client;

void setup() {
  Serial.begin(115200);

  Serial.println("Waiting for connection to WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print('.');
  }
  Serial.println();
  Serial.println("Connected to WiFi network.");

  Serial.println("Starting connection to server...");
  if (client.connect(server, 80)) {
    Serial.println("connected to server");

    WiFiClient client2 = client;
    client2.stop();
    delay(3000);
    if (client.connected()) {
      Serial.println("still connected");
    }
  }
}

void loop() {
}

@P-R-O-C-H-Y
Copy link
Member

Did you test the sketch you posted? We are still getting same result with/without PR changes.

@JAndrassy
Copy link
Contributor Author

tested and retested now. the difference is, that without the PR it prints "still connected"

@P-R-O-C-H-Y
Copy link
Member

tested and retested now. the difference is, that without the PR it prints "still connected"

My bad, I have my environment for testing set wrongly. Its fine now

@me-no-dev me-no-dev added Status: Pending Merge Pull Request is ready to be merged and removed Status: Review needed Issue or PR is awaiting review labels May 10, 2024
@me-no-dev me-no-dev merged commit e8e251a into espressif:master May 10, 2024
134 checks passed
@JAndrassy JAndrassy deleted the networkclient_fix_stop branch May 10, 2024 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: WiFi Issue related to WiFi Status: Pending Merge Pull Request is ready to be merged Type: CI & Testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants