Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Modify socket example to send message to all connected clients

Would anyone know how I could modify this program so that the server sends out messages to all connected clients instead of just the client that send the message in the first place.

http://www.codeproject.com/KB/IP/singleServerMulClient.aspx?display=PrintAll&fid=80315&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2541204&fr=251#xx0xx

I know this is not a simple question but maybe someone could help me out.

Thanks
0
Wanting2LearnMan
Asked:
Wanting2LearnMan
  • 6
  • 3
1 Solution
 
Maxim_VatkinCommented:
Build with winSeverForMultipleClient.cpp
0
 
Wanting2LearnManAuthor Commented:
No I can get it to build properly and I can get several clients to connect to it successfully.

The way it is designed is that when a client sends a message to the server the server sends the same message back to the client which sent it.  I want to make it send the message to all connected clients and I'm not sure how to do this as I have to modify the code.

Thanks for your reply.

0
 
Maxim_VatkinCommented:
1. You need to put serverArgument pointer to each connected client. It can be done at connection creation time. Somewhere in the following code block
            if ( currNumOfClients < MAX_NUM_CLIENTS-1 )
            {
                  clientArgument[currNumOfClients] = new myThreadArgument(client,coutSemaphore,clientName);
                  clientHandle[currNumOfClients] = new myThread(clientHandleThread,(void*)clientArgument[currNumOfClients]);
                  serverArgument->addClientArgument(clientArgument[currNumOfClients]);
                  clientHandle[currNumOfClients]->execute();
                  currNumOfClients++;
            }
 2. Then you can the following code in the clientHandleThread()
            for ( int i = 0; i < MAX_NUM_CLIENTS; i ++ )
            {
                  myThreadArgument* clientInfo = serverArgument->getClientArgument(i);
                  if ( clientInfo )
                  {
                        /* Send message */
                  }
            }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Wanting2LearnManAuthor Commented:
How can I put the server argument inthis block?

if ( currNumOfClients < MAX_NUM_CLIENTS-1 )
            {
                  clientArgument[currNumOfClients] = new myThreadArgument(client,coutSemaphore,clientName);
                  clientHandle[currNumOfClients] = new myThread(clientHandleThread,(void*)clientArgument[currNumOfClients]);
                  serverArgument->addClientArgument(clientArgument[currNumOfClients]);
                  clientHandle[currNumOfClients]->execute();
                  currNumOfClients++;
            }


Thanks for your efforts.
0
 
Maxim_VatkinCommented:
1. Change class as it contain pointer to serverArgument
class myThreadArgument
{

private:
      myThreadArgument *serverArgument;
      ...
}

2. Change constructor  of the class as it accept  pointer to serverArgument

myThreadArgument(myTcpSocket* client,mySemaphore* coutSem,string& cName, myThreadArgument *_serverArgument)
{
        .....
      serverArgument = _serverArgument;
        ......
}
0
 
Wanting2LearnManAuthor Commented:
Then just change the top line of below:
if ( currNumOfClients < MAX_NUM_CLIENTS-1 )
{
     clientArgument[currNumOfClients] = new myThreadArgument(client,coutSemaphore,clientName,    
     serverArgument);

     //blah
     //blah
}

Thanks
0
 
Wanting2LearnManAuthor Commented:
I will check this out and then close off and award points if I have no more questions.

Thanks for all your help.
0
 
Wanting2LearnManAuthor Commented:
I followed your guidelines and added the following and it now send messages to all connected clients.

for ( int i = 0; i < MAX_NUM_CLIENTS; i ++ )
{
                  myThreadArgument* clientInfo = clientArgument->serverArgument->getClientArgument(i);
                  if ( clientInfo )
                  {
      clientInfo->clientConnection->sendMessage(string(messageFromClient));
                  }
            }
}

Many thanks for your help, you are the man !
0
 
Wanting2LearnManAuthor Commented:
Excelllent response.  Maxim_Vatkin was able to look as a whole project and within minutes offered a solution that solved my problem.
Maxim_Vatkin is a true expert.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now