2017-07-19 634 views
1

我尝试使用LwIP netconn API(在stm32f4发现板上)建立多个同时连接。他们都在自己的线程和完美的工作。但由于某种原因,只能同时建立一个连接。使用netconn同时处理多个LwIP连接

我的代码是基于ST回声服务器的例子,看起来像这样:

void myTaskStart(void const * argument) 
{ 
    struct netconn *conn, *newconn; 
    err_t err, accept_err; 
    struct netbuf* buf; 
    void* data; 
    u16_t len; 
    err_t recv_err; 

    /* Create a new connection identifier. */ 
    conn = netconn_new(NETCONN_TCP); 
    if (conn != NULL) 
    { 
     err = netconn_bind(conn, NULL, <some port>); 

     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) 
       { 
         <do stuff here> 

        netconn_close(newconn); 
        netconn_delete(newconn); 
       } 
      } 
     } 
     else 
     { 
      netconn_delete(newconn); 
      printf(" can not bind TCP netconn"); 
     } 
    } 
    else 
    { 
     printf("can not create TCP netconn"); 
    } 
} 

所有线程都在听不同的端口。但是如果另一个使用不同端口的连接已经建立,所有其他线程将在netconn_accept处失败。它返回ERR_ABRT这意味着a connection has been aborted: out of pcbs or out of netconns during accept。 我在这里错过了什么?

回答

1

好的。我找到了解决方案。 增加MEMP_NUM_NETBUF和MEMP_NUM_NETCONN使事情工作。