Page 2 of 2

Re: LWIP- mulitple client example

Posted: Wed Sep 20, 2017 11:03 pm
by sukeshak
Thank you for the response. Will run through some tutorials soon.

I changed the loop as well... since it makes better sense :)

Re: LWIP- mulitple client example

Posted: Fri May 25, 2018 9:08 am
by sarpdaltaban
Dear members,

I am new to the forum and I am planning to implement a multi client LWIP server without RTOS.

Mr. Kolban defines an outline for threading as:

int s = socket();
bind(s, port);
listen(backlog);
while(true) {
int newClient = accept(s);
createTaskToHandleClient(newClient);
}

What about without threading?

Kind Regards

Re: LWIP- mulitple client example

Posted: Sat May 26, 2018 3:59 am
by ESP_Sprite
To be somewhat nitpicky: you'd still use the RTOS as the socket layer and the WiFi stack are depending on it. But if you're asking on how to do this without creating tasks for each client: you'd use something like select() to block until there's activity on any of the clients, then handle them appropriately when the select()-call returns.

Re: LWIP- mulitple client example

Posted: Fri Dec 09, 2022 4:06 am
by rsamanez
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#include <WiFi.h>

const char* ssid = "[YOUR-NETWORK-SSID]";
const char* password = "[YOUR-SSID-PASSWORD]";

void TaskClientSocket( void *pvParameters );

WiFiServer wifiServer(80);

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid,password);
Serial.println("Connecting to Wifi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi network");
Serial.println(WiFi.localIP());

wifiServer.begin();
}

void loop() {
WiFiClient client = wifiServer.available();
if( client){
Serial.println("New Client Connected...");
xTaskCreatePinnedToCore(
TaskClientSocket
, "TaskClientSocket"
, 4098
, &client
, 2
, NULL
, ARDUINO_RUNNING_CORE);
}
}

void TaskClientSocket( void *pvParameters )
{
WiFiClient clientHandle = *((WiFiClient*)pvParameters);
for (;;)
{
while(clientHandle.connected()){
while(clientHandle.available()>0){
char c = clientHandle.read();
clientHandle.write(c);
}
vTaskDelay(100);
}
clientHandle.stop();
Serial.println("Client disconnected");
vTaskDelete(NULL);
}
}