How do I create RTCS application with multiple concurrent connections?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I create RTCS application with multiple concurrent connections?

627 Views
DamonGibson
Contributor III

I have created a Modbus TCP Server application as an MQX task, which runs over RTCS TCP/IP.  This task works very well for a single Modbus TCP Client connection.

It detects a connection via accept(), receives Modbus command packets via recv() and sends Modbus response packets via send().  The task also uses closesocket() to close the connection, when either no Modbus Commands are received (timeout) or by detecting that the Modbus TCP Client has closed the connection.

Since accept() always blocks until a connection occurs, I can't easily modify my task to handle multiple concurrent connections.

What is the best (and simplest) way to handle multiple concurrent TCP connections on the same port (TCP port 502 in this case)?

Labels (1)
0 Kudos
1 Reply

334 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Damon... you need Deamon :smileywink: (not a joke).

It consist of a parentServerTask that listens for connections. When a connection is stablished it creates a new childserverTask, but previously it detaches the socket to send it as parameter to the childTask. This way you will have a task for each connection and server will be always free for listening.

The main parts of the code are next:

void serverChildTask(uint_32 initial_data)

{

listensock = socket(PF_INET, SOCK_STREAM, 0);

child_sock = accept(listensock, NULL, NULL);

/* Create a task to look after it */  

      if (RTCS_detachsock(child_sock) == RTCS_OK)

      {

        result = _task_create(0, SERVER_CHILD_TASK, child_sock);

        if (result == MQX_NULL_TASK_ID)

        {

             RTCS_attachsock(child_sock);

             shutdown(child_sock, FLAG_CLOSE_TX);

           printf("\nCould not create server child task.\n\r");

        }

      }

void serverChildTask(uint_32 initial_data)

{

   uint_32               sock = initial_data;

sock = RTCS_attachsock(sock);

...

If you consider this content solved your inquiry please mark it as correct answer.

Regards,

Carlos

0 Kudos