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

Simple CSocket Server Application Support Multiple Clients

Rate me:
Please Sign up or sign in to vote.
1.94/5 (14 votes)
31 Jan 20052 min read 67K   3.6K   27   13
For my knowledge this is really simple and effective server application to handle multiple clients simultaniousy and completely independently.

Sample screenshot

 

 

 

 

 

Introduction

This is a really simple application you to all have greate experience in Socket communication. And not only that how to handle worker threads with CSocket objects to support multiple clients. I have spend weeks, to get an idea about this. I searched in lot of sites, papers, books just to do it in simple way as I do now. But I have to say they help me to understand some form of functionalities in VC++. So finally I put all what I understood into one and created this simple infrastructure application to support multiple clients.

Structure of the application

This is a simple dialog based application. I my Dialog window class I have only two buttons to start and stop the server. Once you click on start server It will create Listner thread to listen to the client. So it always wait at the Accept method of the socket. So once client got connected to my server Accept method of the socket will get fire.

Then I am getting the connection object and I'll retreive the handle for that. SOCKET handle is a global variable just to refer it in my client handler thread. Inside the accept, I am creating my second thread to handle my newly created client socket. So all the messages comming from that client is handled inside of this thread.

There are nother 2 global variables just to keep the track of created threads and and handles. this is very useful when you want to shutdown the sockets and terminate the threads.

This is the heart of the program.

UINT ListenThread(LPVOID pParam)
{
  AfxSocketInit();
  CieGServerDlg *Dlg= (CieGServerDlg*)pParam;
  CSocket Listen;
  CibCommon iCom;
  int iPort;
  CString strPort;
  iPort = atoi(iCom.readFromRegistry("IEGSERVER","PORT_ID","9000"));
  strPort.Format("%d",iPort);
  Listen.Create(iPort);
  hServerHandle = Listen.m_hSocket;
  if(Listen.Listen())
  {
      CliHandleList.RemoveAll();
      CliThreadList.RemoveAll();
      int i=0;
      while(Dlg->m_bRunServer)
      {
          CSocket * pCSock=
new CSocket();
          if(Listen.Accept(*pCSock))
          {
             hClinetHandle = pCSock->Detach();
             CliHandleList.AddHead(&hClinetHandle);
             CliThreadList.AddHead(AfxBeginThread(RequestHandler,0));
             delete pCSock;
          }
          else
          {
             delete pCSock;
          }
      }
  }
  else
  {
      CString strMsg;
      strMsg.Format("Unable to open requested port %s to this server. May be server         running on this port. Please specify another port and start server again.",strPort);
      Dlg->m_ctrlBUTTONStop.EnableWindow(
false);
      Dlg->m_ctrlBUTTONStart.EnableWindow(
true);
      Dlg->m_ctrlSTATICFlag.SetBitmap(Dlg->m_bmpRed);
      Dlg->m_bRunServer =
false;
      AfxMessageBox(strMsg);
      Dlg->ShowWindow(SW_NORMAL); 
  }
return 0;
}

I think you will enjoy this, and I am really thank full to the people of code project to have a site like this to help others and to learn.

From this same prgram you can learn how you can write a program to read and write to windows registry. In my CibCommon class, I have implemented this code to use when ever I
I want to write and read from registry.

If you have questions I am relly glad to help you.

Thank you
Venura

 

 

    

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Sri Lanka Sri Lanka
I am working as a Tech Lead. I love VC++.
I am trying to get into new technologies coming with VC++ and also in other areas too.

Currently I am working in C# .Net as well...

Now I have sound knowledge in C# as well as in VC++.

Comments and Discussions

 
Questionwhere isthe client??? Pin
chamilsanjeewa14-Jul-09 17:36
chamilsanjeewa14-Jul-09 17:36 
AnswerRe: where isthe client??? Pin
venura c.p.w. goonatillake8-Sep-09 0:22
venura c.p.w. goonatillake8-Sep-09 0:22 
GeneralMemory Leak Pin
BigFatBill9-Dec-08 10:03
BigFatBill9-Dec-08 10:03 
GeneralRe: Memory Leak Pin
Indiopotent11-Sep-09 11:27
Indiopotent11-Sep-09 11:27 
GeneralHi... Pin
chidigam16-Nov-07 1:51
chidigam16-Nov-07 1:51 
GeneralRe: Hi... Pin
venura c.p.w. goonatillake16-Nov-07 1:57
venura c.p.w. goonatillake16-Nov-07 1:57 
Generalwhere is ieGServer.tlb Pin
Hanna245-Apr-06 6:08
Hanna245-Apr-06 6:08 
GeneralArchitecture Pin
Anonymous28-Apr-05 7:08
Anonymous28-Apr-05 7:08 
GeneralRe: Architecture Pin
JohnyAppleseed18-Aug-06 1:48
JohnyAppleseed18-Aug-06 1:48 
GeneralCliHandleList Pin
Member 86978322-Mar-05 19:31
Member 86978322-Mar-05 19:31 
I find that the CliHandleList does not store all the socket handle.
I have tried the following code. The print out are all the same which is the last connection's IP and PORT!!

------------------------------------------------------
CSocket pSock;
SOCKET chandle;
CString address;
unsigned int port;
POSITION p;

p=CliHandleList.GetHeadPosition();
while(p!=NULL){
chandle=(SOCKET)CliHandleList.GetNext(p);
pSock.Attach(chandle);
pScok.GetPeerName(address,port);

/* print the address & port */

}
------------------------------------------------------


GeneralNeed Client Application Pin
PrasadDashputre15-Feb-05 23:40
PrasadDashputre15-Feb-05 23:40 
GeneralRe: Need Client Application Pin
Member 86978322-Mar-05 19:32
Member 86978322-Mar-05 19:32 
GeneralRe: Need Client Application Pin
electric00926-May-09 22:24
electric00926-May-09 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.