How do I create RTCS application with multiple concurrent connections?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How do I create RTCS application with multiple concurrent connections?

812件の閲覧回数
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)?

ラベル(1)
0 件の賞賛
返信
1 返信

519件の閲覧回数
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 件の賞賛
返信