65.9K
CodeProject is changing. Read more.
Home

Multiple Client and Single Server in TCP and Broadcast

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.48/5 (7 votes)

Nov 2, 2007

CPOL
viewsIcon

35490

downloadIcon

1437

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.

If you want can changed in code to same message received by all clients

Enchantment:-if server want to send one Client you have developed own.

And special services server had option to send message to all connected client.

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