lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-users] Handling 3 different TCP connections in netconn API mode


From: Rodrigo Garbi
Subject: [lwip-users] Handling 3 different TCP connections in netconn API mode
Date: Fri, 14 Jun 2013 16:18:41 -0300

Hi all!

I'm working with FreeRTOS v7.0.1 and LwIP 1.4.0 on a Freescale Kinetis K60.

In my application, I need to listen to three different TCP ports. There are three
different devices that are supposed to connect to the one Im devoloping, and
I would like them to connect to three different listening ports.

In a past thread of this list called "Multiple connections at the same time (netconns)",
Gregory had posted:

In other words I must to have 
a multiple opened connections waiting for incoming data. Is that true, that I 
must to create task for particular connection?
This is my idea (it's working, but uses too much RAM):

// - Server task -----------------------------------
void server(void *parameters)
{
    struct netconn *conn = NULL;
    struct netconn *newconn = NULL;
    conn = netconn_new(NETCONN_TCP);
    netconn_bind(conn, NULL, 502);
    netconn_listen(conn);
    for (;;)
    {
        newconn = netconn_accept(conn);
        // creating new task
        xTaskCreate( serviceTask, "svc", STACK, (void*)newconn, PRIORITY, NULL) 
)
    }
}

// - Connection service task -----------------------------------
void modbusServiceConn(void *conn)
{
    struct netbuf *inbuf = NULL;
    uint8_t *data = ""
    int len;

    while (inbuf = netconn_recv(conn))
    {
        netbuf_data(inbuf, (void *)&data, (void *)&len);
        // data processing
        netbuf_delete(inbuf);
        inbuf = NULL;
    }
    netconn_delete(conn);
    conn = NULL;
    vTaskDelete( NULL );
}

Regards, 
Gregory


If I run the above code, I can open up to 3 different connection (to same port). 
But,  if I try to make a 4th connection to the TCP listening port (502), it won´t be possible. 
So, that´s the first question: Do you know why can't I open more than 3 connections?

On the other hand, I tried to do something different:
I create two different "server" tasks, listening to two different TCP ports (let´s say 502 and 503).
Then, from a TCP client application I connected to 502 port, and also to 503 port.
But then, when I tried to open a 3rd connection, to one of those two ports, I would get rejected..

Why is that happening? Is there anything I can´t see?

My settings in lwipopts.h are:

#define MEMP_NUM_TCP_PCB        6
#define MEMP_NUM_TCP_PCB_LISTEN 6


And part of my firmware code is:

void tcp_Task( void *pvParameters )
{    
    err_t error;
    uint16 *tcpListenPort;
    struct netconn *conn = NULL;
    struct netconn *newconn = NULL;
    
    tcpListenPort = (uint16*) pvParameters;
            
    conn = netconn_new(NETCONN_TCP);
    
    if( netconn_bind(conn,IP_ADDR_ANY,(uint16)tcpListenPort) != ERR_OK )
    {        
        vTaskDelete(NULL);
    }    
    
    netconn_listen(conn);
    
    for (;;)
    {
        /* waiting for connection from client */
        error = netconn_accept(conn,&newconn);         
        
        if(error==ERR_OK)
        {
            // creating new task
            sys_thread_new( ( char * ) "svc", serviceTask, (void *)newconn, 350,tskIDLE_PRIORITY + 2 );
        }        
    }    
}

void serviceTask (void *conn)
{
    struct netbuf *inbuf = NULL;    
    err_t error;
            
    while( (error = netconn_recv(conn,&inbuf)) == 0 )
    {
        //RX Data Handling
         
    }    
    netconn_delete(conn);
    conn = NULL;
    vTaskDelete( NULL );    
}

Thank you!

Regards,
Rodrigo G.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]