How to create a server capable of handle multiple TCP clients using FreeRTOS, LwIP nectonn API and HAL libraries
As title says I'm trying to create an only one TCP server in a project based on FreeRTOS, HAL libraries, LwIP middleware and netocnn API. I want to develop a server capable to attend about 5/10 clients simultanously using the same port.
I started testing the ST demo and then customizing the server thread.
But this demo works fine for one connection. As the netconn services called by this thread are thread blocking, I wonder how can multiple connection be done.
If demo is this:
1.-server thread implementation:
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err, accept_err;
struct netbuf *buf;
void *data;
u16_t len;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, 7);
if (err == ERR_OK)
{
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1)
{
/* Grab new connection. */
accept_err = netconn_accept(conn, &newconn);
/* Process the new connection. */
if (accept_err == ERR_OK)
{
while (netconn_recv(newconn, &buf) == ERR_OK)
{
do
{
netbuf_data(buf, &data, &len);
netconn_write(newconn, data, len, NETCONN_COPY);
}
while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(newconn);
}
}
}
/*-----------------------------------------------------------------------------------*/
void tcpecho_init(void)
{
sys_thread_new("tcpecho_thread", tcpecho_thread, NULL, DEFAULT_THREAD_STACKSIZE, TCPECHO_THREAD_PRIO );
}
/*-----------------------------------------------------------------------------------*/
2.-This is the main code where startThread is created:
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* STM32F2xx HAL library initialization:
- Configure the Flash ART accelerator on ITCM interface
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 120 MHz */
SystemClock_Config();
Tasks_InitCommon();//mine
Tasks_InitVars();//mine
Tasks_HwInit();//mine
/* Init thread */
osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 5);
osThreadCreate (osThread(Start), NULL);
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
for( ;; );
}
3.-This is startthread demo code where server thread is created:
static void StartThread(void const * argument)
{
/* Create tcp_ip stack thread */
tcpip_init(NULL, NULL);
/* Initialize the LwIP stack */
Netif_Config();
/* Initialize tcp echo server */
tcpecho_init();
/* Notify user about the network interface config */
User_notification(&gnetif);
for( ;; )
{
/* Delete the Init Thread */
osThreadTerminate(NULL);
}
}How could I improve the demo in order to accept more than one connection?
I have seen one suggetion but I think it doesn't fit with my needs: someone suggests to create the same number of server threads (in this case tcpecho_init threads) than number of connection that you expected to have. But I don't need multiple servers with different application ports, I want to do an only one server handling as many uknown number of connections (up to 10) as it is having in run time. I'm not interested on having prefedefined connections (neither x num of servers running).
My hardware platform is a stm32f207ZG microcontroller.
I will appreciate any idea/suggestions/exemples, etc. Thanks for your attention.
