LwIP stack - more than one connection on the same port

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

I have a question regarding TCP connection on LwIP stack. I have to

implement 5 connection on one port. Let me explain what I do:

wiced_tcp_create_socket - create new scocket;

wiced_tcp_listen               - listen on port;

wiced_tcp_accept             - wait for accept in new thread

When connected, I start the same procedure but with completly new socket

and listen on the same port as before function wiced_tcp_listen return

WICED_ERROR. I invastigated that netconn_bind return ERR_USE (-13).

It is possible to get more than one connection on the same port?

0 Likes
5 Replies
Anonymous
Not applicable
I invastigated deeply what LwIP do to achieve more than one connection. First, I discovered that ERR_USE comes from tcp_bind (core/tcp.c), so I commented this and achieved 3 connection on the same port. When appliction try to create new fourth socket, wiced_tcp_create_socket return -1 and "memp_malloc: out of memory in pool NETCONN" appear.

Also, I played with options in LwIP/wwd/FreeRTOS/lwipopts.h and src/include/lwip/opt.h but no success.

How to achieve more than 3 connections? I need 5.
0 Likes
Anonymous
Not applicable
Your second post shows that the failure is due to inadequate memory in the NETCONN pool.

Try increasing MEMP_NUM_NETCONN
0 Likes
Anonymous
Not applicable
Exaclly. I increased MEMP_NUM_NETCONN in lwipopts.h and MEMP_NUM_TCP_PCB in opt.h and reach 5 connection.

This five connections was established between one client (my PC) and AP. But I also need to connect 5 phones (one connection per phone). And here I met problems. When I try to connect 5 phones to AP, 4 of them obtain IP address and connect to AP but fifth not.

Could you explain how to force WICED module to run with 5 phones?
0 Likes
Anonymous
Not applicable
I wonder if my change in LwIP unstable the connections. My changes in LwIP was:

          if (ip_addr_isany(&(cpcb->local_ip)) ||

              ip_addr_isany(ipaddr) ||

              ip_addr_cmp(&(cpcb->local_ip), ipaddr)) {

            // return ERR_USE;  <------------------- HERE, I REMOVE THIS -----------------

          }

lwipopts.h:

#define MEMP_NUM_NETCONN               (16)

opt.h:

#ifndef MEMP_NUM_TCP_PCB

#define MEMP_NUM_TCP_PCB                7

#endif

Could somebody tell me how this changes influence LwIP stable ???
0 Likes
Anonymous
Not applicable
Im not sure how the change affects LwIP - the code is there to make sure theres just a single socket bound to a given address/port.

Currently the WICED SDK doesnt support multiple concurrent TCP sockets - perhaps you could try the following approach: add another wrapper function around netconn_accept() and use separate sockets for the listener and the accepted connections. This way a single listening socket would be kept.

Another approach - closing the listening socket after calling wiced_tcp_accept() and opening/binding a new one might not work well due to TCP timewait (more information and an example of what I meant above: http://lwip.wikia.com/wiki/Netconn_bind).
0 Likes