cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement a TCP server with multiple client connections without RTOS ?

YPara
Associate II

Hi,

I would like to implement a Modbus TCP server based on the library cubeMX and LWIP with the ability of multiple client connections.

With only one client, it works perfectly well. With 2 clients, they can connect simultaneously but as soon as both send simultaneously a request, one of the 2 requests is discarded and it blocks the client.

I checked the LWIP statistics but everything seems ok.

I have to mention that I do not use a RTOS.

Browsing the net, I didn’t find any example of multiple connection without RTOS, does anyone already succeed implementing such behavior?

Many thanks in advance for your help

3 REPLIES 3
Piranha
Chief II

Yes, lwIP works well with and without RTOS.

You must create one listening connection, which stays active all the time and waits for incoming connections. For this listening connection You have to set up a callback function with tcp_accept(). When a new client wants to connect, IP stack creates a new data connection and calls Your accept callback with a pointer to this new data connections struct tcp_pcb. In accept callback You have to set up receive callback for each newly created data connection with tcp_recv() and accept this new connection by returning ERR_OK. Additionally there with tcp_arg() You can attach a custom data structure for each particular data connection, which also will be passed to receive callback function, if You need one.

Look for example at UM1713 section 4.1.2:

https://www.st.com/content/ccc/resource/technical/document/user_manual/65/e8/20/db/16/36/45/f7/DM00103685.pdf/files/DM00103685.pdf/jcr:content/translations/en.DM00103685.pdf

Also at section 5.1 in this:

http://ww1.microchip.com/downloads/en/AppNotes/Atmel-42233-Using-the-lwIP-Network-Stack_AP-Note_AT04055.pdf

YPara
Associate II

​Many thanks for this answer, I will try to follow your advices and rewrite the code in that direction.

Could you confirm that 2 simultaneously running communications (in modbus in this case) is perfectly achievable with LWIP on a stm32F407 microcontroller?

It has been weeks I'm looking for it now and I'm a little despaired of finding a solution.

It's disconcerting, because The 2 clients can be connected simultaneously, they can communicate with the server as well, but as soon as 2 requests (one from each client) arrive at the same time, one of the 2 requests is discarded. It has the consequence to block one of the 2 clients since it never receives the reply to its request.

I enabled the LWIP statistics, but I didn't see anything that can explain such behavior.

What test set up are you using to get this simultaneous connection and ping ?