cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP server example based on LWIP netconn

DRude.1
Associate II

Hi, I create a project on stm32f746 nucleo board using LWIP stack (netconn) and FreeRTOS.

The main function of this project is HTTP server. But I have a few problems with this functionality - they are errors of connection and close states of HTTP session.

Have anybody working project of HTTP server (netconn) which can make a good connection without errors, RST, retransmissions? I am needed a code of stability working server because tested a lot of codes, but all of its works not perfect.

Maybe I did a mistake in this code?

Code which I use:

typedef struct struct_sock_t {
  struct netconn *conn;
} struct_sock;
struct_sock sock;
 
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
  /* USER CODE BEGIN 5 */
  //-----------------------------------------------------------
  struct netconn *conn;
  err_t err;
 
  conn = netconn_new(NETCONN_TCP);
  if(conn!=NULL)
  {
	  sock.conn = conn;
	  err = netconn_bind(conn, NULL, 80);
	  if(err == ERR_OK)
	  {
		  netconn_listen(conn);
		  sys_thread_new("tcp_thread", tcp_thread, (void*)&sock, DEFAULT_THREAD_STACKSIZE, osPriorityNormal);
	  }
	  else
	  {
		  netconn_delete(conn);
		  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_SET);             // LED RED
	  }
  }
  for(;;)
  {
	  osDelay(1);
  }
  //-----------------------------------------------------------
  /* USER CODE END 5 */
}
static void tcp_thread(void *arg)
{
	err_t recv_err;                      // Variables for arguments (errors)
	err_t err;                           // Variables for arguments (errors)
	struct netconn *conn;                // Structure for connection
	struct netconn *newconn;             // Structure for connection
	struct netbuf *inbuf;                // Pointer to connection buffer
	struct_sock *arg_sock;               // Pointer to arguments of task
	arg_sock = (struct_sock*) arg;
	conn = arg_sock->conn;
	u16_t buflen;
	char* buf;
	struct fs_file file;
	for(;;)
	{
		err = netconn_accept(conn, &newconn);
	    if (err == ERR_OK)
	    {
	    	recv_err = netconn_recv(newconn, &inbuf);                     // Receiving the socket data in inbuf
		    if (recv_err == ERR_OK)
		    {
		    	if (netconn_err(newconn) == ERR_OK)
		    	{
		    		netbuf_data(inbuf, (void**)&buf, &buflen);
		    	    if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
		    	    {
		    	    	if ((strncmp((char const *)buf,"GET / ",6)==0)||(strncmp((char const *)buf,"GET /index.html",15)==0))
		    	    	{
		    	    		fs_open(&file, "/index.html");
		    	    		netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
		    	    		fs_close(&file);
		    	    	}
		    	    	else if (strncmp((char const *)buf,"GET /IMG/Test.png",17)==0)
		    	    	{
		    	    		fs_open(&file, "/IMG/Test.png");
		    	    		netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
		    	    		fs_close(&file);
		    	    	}
		    	    	else
		    	    	{
		    	    		fs_open(&file, "/404.html");
		    	    		netconn_write(newconn, (const unsigned char*)(file.data), (size_t)file.len, NETCONN_NOCOPY);
		    	    		fs_close(&file);
		    	    	}
		    	    }
		    	    else
		    	    {
		    	    	osDelay(1);
		    	    }
		    	}
		    	else
		    	{
		    		osDelay(1);
		    	}
		    }
		    netbuf_delete(inbuf);
		    netconn_close(newconn);
		    netconn_delete(newconn);
	    }
	    else
	    {
	    	osDelay(1);
	    }
	}
}

0693W00000AMsplQAD.png0693W00000AMspbQAD.png

10 REPLIES 10

@Piranha​ I also thought I am a developer before I start work with LWIP... Seriously, I am self-taught, my specialization is not programming. But I need to set LWIP on stm32. And it hard enough for me, make it works ideal, not make it just works. So I do not have information on how it does without CubeMX (FreeRTOS and LWIP), because it the most popular way, and almost all information on the internet is just about it. You show the problems LWIP, but the information for the decision is available for a narrow circle of specialists, not the whole community