Click here to Skip to main content
15,891,253 members
Articles / Desktop Programming / MFC
Article

Multiple Client and Single Server in TCP and Broadcast

Rate me:
Please Sign up or sign in to vote.
1.48/5 (7 votes)
1 Nov 2007CPOL 35.3K   1.4K   9  
Multiple Client and Single Server in TCP and Broadcast
Screenshot - Message.jpg

Introduction

Description:-This Server will accept maximum 10 Client. Client sends message to server. Server will broadcast same message to connected all client expect who send. Here I am using word "broadcast" in TCP Stream. Generally broadcast is used in UDP stream.<o:p>

If you want can changed in code to same message received by all clients<o:p>

Enchantment:-if server want to send one Client you have developed own.<o:p>

And special services server had option to send message to all connected client.<o:p>

void CTestServerSocketDlg::OnDataReceived(WPARAM wParam,LPARAM lParam) 
{
 try
 {    
   CString csTemp; 
   int mnInstanceNo = (int)lParam; // client number who send data 
   CClientSocket  * pCheckSocket;  
   pCheckSocket = (CClientSocket *  ) m_ClientSocketArray.GetAt(mnInstanceNo); 
   
   char szBuff[1024]; 
   memset(szBuff,'\0',sizeof szBuff); 
   pCheckSocket->Receive(szBuff,1024);
   csTemp.Empty(); 
   csTemp.Format("%s",szBuff); 
   m_ctrlDataRec.InsertString(-1,csTemp); 
   UpdateData(false); 
   for ( int i=0;i<MAX_CONNECTION;i++) 
   {
    pCheckSocket = (CClientSocket * )m_ClientSocketArray.GetAt(i); 
    if(pCheckSocket) 
     {    if(pCheckSocket->m_nInstanceNo != mnInstanceNo) 
    {pCheckSocket->Send(szBuff,sizeof szBuff);}
      }
    }// end for
  }// end try
 catch(...) { }
Screenshot - server.jpg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I am working at Hyderabad as vc++ software developer,I passed B.Com and B-level from DOEACC (www.doeacc.org.in)through O.U.Campus.

Comments and Discussions

 
-- There are no messages in this forum --