Skip to main content
Visitor II
April 3, 2025
Question

One HTTP server and two TCP servers with different ports using FreeRTOS + LWIP

  • April 3, 2025
  • 3 replies
  • 633 views

I am trying to implement the HTTP server and two TCP servers on Port 5000 and 5001 using FreeRTOS, but what I notice is if I try to connect all these three simultaneously it is failing, only gets to connect if other two are not connected.

Controller used: stm32f407vgt6
FreeRTOS API: CMSIS v2
Clock configuration: 168Mhz with source HSI

I am using Hercules to check the connection over TCP.
While debugging I am not getting any hardfault errors too. 
I tried increasing the size, and also by changing priorities. But I am not able to work this out.

My tcpserver2.c is exactly same as tcpserver1.c except for the port address.
 tcpserver1.c

#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/sys.h"

#include "tcpserver.h"
#include "string.h"
static struct netconn *conn, *newconn;
static struct netbuf *buf;
static ip_addr_t *addr;
static unsigned short port;
char msg[100];
char smsg[200];


/**** Send RESPONSE every time the client sends some data ******/
static void tcp_thread1(void *arg)
{
	err_t err, accept_err, recv_error;

	/* Create a new connection identifier. */
	conn = netconn_new(NETCONN_TCP);

	if (conn!=NULL)
	{
		/* Bind connection to the port number 7. */
		err = netconn_bind(conn, IP_ADDR_ANY, 5000);

		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)
				{

					/* receive the data from the client */
					while (netconn_recv(newconn, &buf) == ERR_OK)
					{
						/* Extrct the address and port in case they are required */
						addr = netbuf_fromaddr(buf); // get the address of the client
						port = netbuf_fromport(buf); // get the Port of the client

						/* If there is some data remaining to be sent, the following process will continue */
						do
						{

							strncpy (msg, buf->p->payload, buf->p->len); // get the message from the client

							// Or modify the message received, so that we can send it back to the client
							int len = sprintf (smsg, "\"%s\" was sent by the Server1\n", msg);

							netconn_write(newconn, smsg, len, NETCONN_COPY); // send the message back to the client
							memset (msg, '\0', 100); // clear the buffer
						}
						while (netbuf_next(buf) >0);

						netbuf_delete(buf);
					}

					/* Close connection and discard connection identifier. */
					netconn_close(newconn);
					netconn_delete(newconn);
				}
			}
		}
		else
		{
			netconn_delete(conn);
		}
	}
}

void tcpserver1_init(void)
{
 sys_thread_new("tcp_thread1", tcp_thread1, NULL, DEFAULT_THREAD_STACKSIZE,osPriorityBelowNormal);
// sys_thread_new("tcp_thread2", tcp_thread2, NULL, DEFAULT_THREAD_STACKSIZE,osPriorityBelowNormal);
}

 

main.c

#include "main.h"
#include "cmsis_os.h"
#include "lwip.h"
#include "httpserver.h"
#include "tcpserver.h"
#include "tcpserver2.h"

osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
 .name = "defaultTask",
 .stack_size = 256 * 4,
 .priority = (osPriority_t) osPriorityNormal,
};

void StartDefaultTask(void *argument);

int main(void)
{
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 MX_UART5_Init();
 MX_I2C3_Init();
 osKernelInitialize();
 defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
 osKernelStart();
 while (1)
 {
 }
}

void StartDefaultTask(void *argument)
{
 /* init code for LWIP */
 MX_LWIP_Init();
 /* USER CODE BEGIN 5 */
 http_server_init();
 tcpserver1_init();
 tcpserver2_init();
 /* Infinite loop */
 for(;;)
 {
 osDelay(100);
 }
}


 

    This topic has been closed for replies.

    3 replies

    Super User
    April 3, 2025

    What IP stack are you using?

    Have you configured it for multiple connections?

    Visitor II
    April 3, 2025

    I am using the LWIP
    and for multiple connections the defines are as:
    #define MEMP_NUM_TCP_PCB       5
    #define MEMP_NUM_NETCONN    4 

    Visitor II
    April 3, 2025

    I also have tried increasing the value, but it didnt work.