Page 1 of 1

LWIP multiple connections

Posted: Thu Feb 06, 2014 3:57 pm
by tombalabomba
Hi everyone!

I desperately try to establish three TCP/IP connections (Port 12345, 7 and 80) to a STM32 Discovery board (extended by a ethernet phy, ChibiOs 2.6.2 / LWIP1.4.1).
Each connection is handled by it's own thread.

Unfortunatly, only one connection can be established at the same time.

The code for all three threads looks similar like :

Code: Select all

    struct netconn *conn, *newconn;
    struct netbuf *rx_buffer;
    uint8_t *buf;
    u16_t buflen;
    err_t err;

    /* Create a new TCP connection handle */
    conn = netconn_new(NETCONN_TCP);
    LWIP_ERROR("echo_server: invalid conn", (conn != NULL), return RDY_RESET;);

    /* Bind to port */
    netconn_bind(conn, NULL, ECHO_THREAD_PORT);
   
    /* Put the connection into LISTEN state */
    netconn_listen(conn);

    /* Goes to the final priority after initialization.*/
    chThdSetPriority(ECHO_THREAD_PRIORITY);
   

    while (1) {

        /* Blocks the process until a connection request from a remote host arrives.
           netconn_listen() must be called prior to netconn_accept(). */
        err = netconn_accept(conn, &newconn);
     
        if (err != ERR_OK) {
            continue;
        }

        /* Set receive timeout */
        netconn_set_recvtimeout (newconn, ECHO_RECV_TIMEOUT);

        while (1) {

            /* Read data from echo port */
            err = netconn_recv(newconn, &rx_buffer);

           ...



I found out, that netconn_accept blocks if another connection using a different port has been established.


Any help will by appreciated.

Re: LWIP multiple connections

Posted: Thu Feb 06, 2014 4:14 pm
by Giovanni
This is more lwIP-related, personally I never went so far in using it. Let's wait more answers.

Giovanni

Re: LWIP multiple connections

Posted: Fri Feb 07, 2014 12:38 am
by DeusExMachina
May be you need provide more memory to lwip?
As for me I successfully used 2 simultaneously connections on 80 and 501 port + ping. Everything worked fine.

Re: LWIP multiple connections

Posted: Fri Feb 07, 2014 11:25 am
by tombalabomba
I increased LWIP_THREAD_STACK_SIZE in lwipthread.h and MEM_SIZE in lwipopts.h by factor of ten.
Unfortunatly, this makes no difference. When a connection at port 12345 is established, the connection at port 7 fails.

By the way, netconn_accept doesn't return, it stays blocked. If there is a problem with memory, I guess it would return with error code -1.

I didn't found much code using lwip with multiple simultaneously opened connections. Is there something the libary has to be configured for?
MEMP_NUM_TCP_PCB is 5 in lwipopts.h, so I guees it should be possible.

Thanks

Re: LWIP multiple connections

Posted: Thu Feb 27, 2014 11:14 am
by tombalabomba
Hi everyone!

Because I'm still suffering the same problem I should ask some fundamental questions.

Is it possible to use 2 connections in two different threads? Or must I handle both connections in a single thread?
As can be seen a in the code snippet of main.c I created three thread. One for lwip, one for communication at port 12345 and a webserver thread for port 80.

Code: Select all

int main(void)
{

    /* IP settings */
    uint8_t macaddress[]  = {0x02,0x00,0x00,0x00,0x00,0x00};
    struct lwipthread_opts lwip_configuration;


    /*
     * System initializations.
     * - HAL initialization, this also initializes the configured device drivers
     *   and performs the board-specific initializations.
     * - Kernel initialization, the main() function becomes a thread and the
     *   RTOS is active.
     */
    halInit();
    chSysInit();

    /* Hardware initialization */
    hardware_init();



    /*
     * Creates output handler thread.
     */
    chThdCreateStatic(wa_output_handler,
                      sizeof(wa_output_handler),
                      HIGHPRIO,
                      output_handler,
                      NULL);

    /*
     * Creates the LWIP threads (it changes priority internally).
     */
    macaddress[4] = serial;
    macaddress[5] = hardware_address;
    lwip_configuration.macaddress = macaddress;
    IPADDR(lwip_configuration.address, 192, 168, 55, hardware_address);
    IPADDR(lwip_configuration.netmask, 255, 255,  0,  0);
    IPADDR(lwip_configuration.gateway, 192, 168,  0,  1);
   
    chThdCreateStatic(wa_lwip_thread,
                      LWIP_THREAD_STACK_SIZE,
                      NORMALPRIO + 1,   
                      lwip_thread,
                      &lwip_configuration);
   
    /*
     * Creates the comm thread (it changes priority internally).
     */
    chThdCreateStatic(wa_comm_thread,
                      sizeof(wa_comm_thread),
                      NORMALPRIO,
                      comm_thread,
                      NULL);


    /*
     * Creates the HTTP thread (it changes priority internally).
     */
    chThdCreateStatic(wa_http_server,
                      sizeof(wa_http_server),
                      LOWPRIO,
                      http_server,
                      NULL);



The code for the webserver looks like :

Code: Select all

msg_t http_server(void *p) {
  struct netconn *conn, *newconn;
  err_t err;

  (void)p;

  /* Create a new TCP connection handle */
  conn = netconn_new(NETCONN_TCP);
  LWIP_ERROR("http_server: invalid conn", (conn != NULL), return RDY_RESET;);

  /* Bind to port 80 (HTTP) */
  netconn_bind(conn, NULL, WEB_THREAD_PORT);

  /* Put the connection into LISTEN state */
  netconn_listen(conn);

  /* Goes to the final priority after initialization.*/
  chThdSetPriority(WEB_THREAD_PRIORITY);

  while(1) {
    err = netconn_accept(conn, &newconn);

    if (err != ERR_OK) {
      continue;
    }



and similar the code for the communication at port 12345

Code: Select all

/**
 * comm server thread.
 */
/*-------------------------------------------------------------------------*/
msg_t comm_thread(void *p) {

    struct netconn *conn, *comm_conn;
    err_t err;
    (void)p;



    /* Create a new TCP connection handle */
    conn = netconn_new(NETCONN_TCP);
    LWIP_ERROR("comm_server: invalid conn", (conn != NULL), return RDY_RESET;);

    /* Bind to port 12345 */
    netconn_bind(conn, NULL, COMM_THREAD_PORT);

    /* Put the connection into LISTEN state */
    netconn_listen(conn);

    /* Goes to the final priority after initialization.*/
/*    chThdSetPriority(COMM_THREAD_PRIORITY);*/



    while (1) {

        /* Blocks the process until a connection request from a remote host arrives.
           netconn_listen() must be called prior to netconn_accept(). */
        err = netconn_accept(conn, &comm_conn);
     
        if (err != ERR_OK) {
            continue;
        }



The problem is, that if one connection is established, netconn_accept() doesn't return for the other thread.
May there is a special setting in lwipopts.h. I searched for weeks after this problem without any improvement.

So, I guess I did a fundamental mistake. Has anyone experience with Lwip using simultaniously multiple ports?

lG