Thank you for the response. Will run through some tutorials soon.
I changed the loop as well... since it makes better sense
LWIP- mulitple client example
-
- Posts: 4
- Joined: Fri May 25, 2018 9:01 am
Re: LWIP- mulitple client example
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
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
-
- Posts: 9766
- Joined: Thu Nov 26, 2015 4:08 am
Re: LWIP- mulitple client example
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
#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);
}
}
#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);
}
}
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 162 guests