cancel
Showing results for 
Search instead for 
Did you mean: 

How can I enable multiple clients to send queries to an Azure RTOS NetX TCP/IP server on an STM32H5

Eakyo.1
Associate III

In my project, I have a TCP/IP server application running. My main problem is that only one client can connect to the same IP and port at the same time. My project requirement is that multiple clients should be able to send queries to the same IP and port. I can achieve this structure with LWIP, but I couldn't set up the same structure with Azure RTOS NetX. One client works without a problem, but multiple clients cannot send queries. What can we do about this?

6 REPLIES 6
Eakyo.1
Associate III

I set the value of NX_MAX_PHYSICAL_INTERFACES to 4 and used the nx_ip_interface_attach function. I tried this in the TCP/IP server echo project you shared for the STM32H5, and only one client could connect to the same IP and port. Multiple connections are not possible. If you have a solution for this, could you please share it with me in the TCP/IP server echo project you provided?

BarryWhit
Senior

The same principles apply to NetX as they do in LWIP or any Sockets interface. 

If you want to handle multipl connections concurrently, then after the server calls the equiv of `accept()`

you have to spawn a new thread to handle the communication on that connection. That way, the main thread

can call immediately call listen() again and be ready for a new connection.

 

In the Echo Server example, after the call to `nx_tcp_server_socket_accept`, the main thread goes into a while(1) loop and talks to the connected client until it disconnects.

 

There is a "cheat" which may or may not suit your needs. The socket interface usually support a configurable-length "queue" of pending clients. So that if a client tries to connect while the server is dealing with another connection, hey are not refused, they simply block (on their side). If you can guarantee that each client will connect, have a brief exchange with the server, and then disconnect, you can get away with having just one main thread. But be this usually ends up not being good enough. It also isn't resilient to any errors (if a client dies, you server will stall until a timeout occurs, for example, which can be many seconds or even more).

 

Newer network stacks (in general) use an "event-loop" and an async model of network programming, I don't know if the NetX API supports something like this. These APIs tend to scale better and use less memory (maybe), but there's a learning curve and it can be harder to debug your code.

- If you feel a post has answered your question, please click "Accept as Solution".
- Once you've solved your issue, please consider posting a summary with any additional details you've learned. Your new knowledge may help others in the future.

Hi @BarryWhit ,

Thank you for your feedback. As I understand it, to handle multiple connections concurrently in the NetX API, we need to spawn a new thread after each connection acceptance. I have a few questions to better understand and implement this method:

Example Code: Could you provide an example code snippet for starting a new thread after the nx_tcp_server_socket_accept function and managing the connection within this thread? This would help me integrate this structure into my current Echo Server project.

Event-Loop and Asynchronous Programming: You mentioned that newer network stacks support an event-loop and an asynchronous model of network programming. Could you provide more information on whether the NetX API supports these features? If it does, could you guide me on how to implement it in my current project or suggest some resources?

I would appreciate your feedback on these topics. Thank you again for your help.

Best regards,

 
BarryWhit
Senior

No. You'll have to do that yourself.

- If you feel a post has answered your question, please click "Accept as Solution".
- Once you've solved your issue, please consider posting a summary with any additional details you've learned. Your new knowledge may help others in the future.
STea
ST Employee

Hello @Eakyo.1 ,

This issue was addressed in this thread How to tune azure netxduo to receive more incoming... - STMicroelectronics Community.


Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.