Search code examples
multithreadingtcpstm32freertoslwip

Handling multiple LwIP connections at the same time using netconn


I try to establish several simultaneous connections using LwIP netconn API (on stm32f4 discovery board). All of them are in their own threads and work perfectly. But for some reason only one connection can be established at the same time.

My code is based on ST echo server example and looks like this:

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");
    }
}

All threads are listening to different ports. But if another connection which uses a different port has been already established all other threads fail at netconn_accept. It returns ERR_ABRT which means a connection has been aborted: out of pcbs or out of netconns during accept. What do I miss here?


Solution

  • Ok. I found the solution. Increasing MEMP_NUM_NETBUF and MEMP_NUM_NETCONN made things work.