Skip to main content
Explorer
July 19, 2022
Question

lwip accept fails after staying "idle" for sometime, how can I fix this?

  • July 19, 2022
  • 2 replies
  • 1117 views

Hello STM community,

I am developing server/client application.

Client is running on Windows 11, using winsock2.h and ws2tcpip.h library

Server is running on STM32F429ZIT6 - Nucleo

If I reset MCU, I have no issue establishing a connection, send & receive on both end.

If MCU stays in lwip_accept method for too long, client is not able to establish connection.

void StartDefaultTask(void *argument)
{
 /* init code for LWIP */
 MX_LWIP_Init();
 /* USER CODE BEGIN 5 */
 
	struct sockaddr_in address, client_addr;
	socklen_t sin_size;
	int s_create, new_socket;
	int addrlen = sizeof(address);
	int opt = 1;
	int socket_check;
	int		 		 retVal;
 
 
	s_create = lwip_socket(AF_INET, SOCK_STREAM, 0);
 
	memset(&address,0,sizeof(address));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = htonl(IPADDR_ANY);
	address.sin_port = htons(HTTP_PORT); 
 
	socket_check = lwip_bind(s_create, (struct sockaddr*)&address, sizeof(address));
 
	socket_check = lwip_listen(s_create, 1);	//returns 0 on succes, -1 on failure
 
	uint8_t elyes[6] = {70,114,101,110,99,104};
	uint8_t data[100] = {'\0'};
	char recvBuff[100];
	memset(&recvBuff,0,sizeof(recvBuff));
 
	char printMsg[100];
 for(;;)
 {
	 sin_size = sizeof(struct sockaddr_in);
	 new_socket = lwip_accept(s_create, (struct sockaddr *)&client_addr, &sin_size); //Blocking function, ping doesnt work
	 if (new_socket > 0){
		 uint16_t msgSize = sprintf(printMsg, "new client connected from IP: %s, Port: %d\r\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
		 HAL_UART_Transmit(&huart3, (uint8_t*)printMsg, msgSize, 10);
		 sys_thread_new("process msg",
				 	 	 socket_thread,
						 (void*)new_socket,
						 THREAD_STACKSIZE,
						 DEFAULT_THREAD_PRIO);
	 }
 
	 osDelay(1000);
 
 }
 /* USER CODE END 5 */
}
 
 
void socket_thread(void *ptr){
	int cSocket = (int)ptr;
	int retVal;
 
	uint8_t elyes[6] = {70,114,101,110,99,104};
	uint8_t data[100] = {'\0'};
	char recvBuff[100];
 
	while(1){
		retVal = lwip_write(cSocket, elyes, sizeof(elyes));
		if(retVal < 0) break;
 
		retVal = lwip_read(cSocket, &recvBuff, sizeof(recvBuff));	//return number of bytes received.
		if(retVal < 0) break;
	}
 
	osDelay(500);
	close(cSocket);
	vTaskDelete(NULL);
}

I am thinking there is some conflict between FreeRTOS and lwip_accept() method but i'm not sure. I can see request packets from client with WireShark when Server fails to respond. That's why i'm pretty confident its lwip_accept() that fails.

I am using CMSIS_V2 Interface for FreeRTOS

Any Idea why ?

    This topic has been closed for replies.

    2 replies

    DPatr.2Author
    Explorer
    July 20, 2022

    I switched to CMSIS_V1 and that fixed my issue with lwip_accept

    Hopefully this can help some concerned soul about this FreeRTOS/lwip stack issue.