?
Solved

Modify socket example to send message to all connected clients

Posted on 2010-01-12
9
Medium Priority
?
413 Views
Last Modified: 2012-06-27
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
Comment
Question by:Wanting2LearnMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 2

Expert Comment

by:Maxim_Vatkin
ID: 26293045
Build with winSeverForMultipleClient.cpp
0
 

Author Comment

by:Wanting2LearnMan
ID: 26293106
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
 
LVL 2

Expert Comment

by:Maxim_Vatkin
ID: 26293356
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:Wanting2LearnMan
ID: 26293520
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
 
LVL 2

Accepted Solution

by:
Maxim_Vatkin earned 980 total points
ID: 26293867
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
 

Author Comment

by:Wanting2LearnMan
ID: 26294014
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
 

Author Comment

by:Wanting2LearnMan
ID: 26294229
I will check this out and then close off and award points if I have no more questions.

Thanks for all your help.
0
 

Author Comment

by:Wanting2LearnMan
ID: 26324663
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
 

Author Closing Comment

by:Wanting2LearnMan
ID: 31676068
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question