How to send data to all TCP Clients from TCP Server?

Hi all,

I’ve created a TCP Server inside Q2687 series module and it works fine. I also developed a desktop Client App to connect to the Server. I can connect to the Server with multiple Celient App at the same time. For some reason, I need to send data to all “connected TCP Clients” from the Server but I could not find an API or command for this. Is there any way to do this? (This is the main problem, if there is a way to do it, I won’t use the below method to solve the problem.)

So, I tried to solve the problem with my custom way. There should be used a linked list structure because of automatic list management advantages (add, remove, etc.). I decided to use the list API of Open AT Framework (wm_lst_t) to store TCP Clients’ ID’s to send them data manually. When a Client connect to the Server, I am adding the Client ID to list as follows;

position = wm_lstAddItem ( ListPtr, ev->channel ); //ev->channel gives me an ID like “0x180ce034”

And, when a Client disconnected (by the event WIP_CEV_PEER_CLOSE) or got an Error (by the event WIP_CEV_ERROR), I am deleting the Client from the list as follows;

clientCount = wm_lstDeleteItem ( ListPtr, wm_lstFindItem ( ListPtr, ev->channel ) );

From the above code line, the API wm_lstFindItem should find and give me the ID of current Client that need to be deleted from the list. It doesn’t work :interrobang: It always give 0 as index. (This is my second problem)

Any idea that you give me to solve one of the two problems would be appreciated.

Thanks in advance.

MÇ.

Hi all,

Does anyone have idea? Actually, this is a very common issue and there should be an easy way for this? :frowning_face:

MÇ.

Hi,

I don’t know why but I could not use the list API of OpenAT, it gives me error and resetting my modem each time when a client disconnected from the server so I solved the problem with my custom linked list. I am posting the solution below for those people who got similar problem for the future :slight_smile:

In the global variable section:

typedef struct node {
wip_channel_t val;
struct node * next;
} node_t;

node_t *head = NULL;

To Add Client to the Linked List Method:

void AddClientToList ( node_t * h, wip_channel_t v )
{
node_t *current = h;

while (current->next != NULL)
    current = current->next;

current->next = malloc(sizeof(node_t));
current->next->val = v;
current->next->next = NULL;

}

To Remove Client from the Linked List Method:

node_t * RemoveClientFromList ( node_t *h, wip_channel_t v )
{
node_t *temp = NULL;
node_t *current = h;

if (h->val == v)
{
	temp = h;
	h = h->next;
	free(temp);
	return h;
}

while (current->next != NULL && current->next->val != v)
	current = current->next;

if (current->next == NULL)
	return h;

temp = current->next;
current->next = current->next->next;
free(temp);
return h;

}

To Show Connected Clients into the Linked List:

void ShowConnectedClients ( node_t *h )
{
u8 i = 1;
node_t *current = h;

while (current != NULL)
{
if ( isDebug )
TRACE ( ( NORMAL_TRACE_LEVEL, “Client %d: 0x%x.”, i, current->val ) );

  current = current->next;
  i++;

}
}

Into the WIP_CEV_OPEN event of TCP server (when a TCP Client Connected to the Server):

if (clientCount == 0)
{
head = malloc ( sizeof ( node_t ));
head->val = ev->channel;
head->next = NULL;
}
else
AddClientToList ( head, ev->channel );

ShowConnectedClients ( head );
clientCount++;

Into the WIP_CEV_ERROR and WIP_CEV_PEER_CLOSE events (When a TCP Client Disconnected from the TCP Server):

head = RemoveClientFromList ( head, ev->channel );
ShowConnectedClients ( head );

To send data to all Clients:

ascii msg[4] = “test”;
node_t *current = head;
while (current != NULL)
{
wip_write ( current->val, msg, wm_strlen ( msg ) );
current = current->next;
}

Best regards,

MÇ.