Click here to Skip to main content
Click here to Skip to main content

Simple CSocket Server Application Support Multiple Clients

, 31 Jan 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

venura c.p.w. goonatillake
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??? Pinmemberchamilsanjeewa14-Jul-09 17:36 
AnswerRe: where isthe client??? Pinmembervenura c.p.w. goonatillake8-Sep-09 0:22 
GeneralMemory Leak PinmemberBigFatBill9-Dec-08 10:03 
GeneralRe: Memory Leak PinmemberIndiopotent11-Sep-09 11:27 
GeneralHi... Pinmemberchidigam16-Nov-07 1:51 
GeneralRe: Hi... Pinmembervenura c.p.w. goonatillake16-Nov-07 1:57 
Generalwhere is ieGServer.tlb PinmemberHanna245-Apr-06 6:08 
GeneralArchitecture PinsussAnonymous28-Apr-05 7:08 
GeneralRe: Architecture PinmemberJohnyAppleseed18-Aug-06 1:48 
GeneralCliHandleList PinmemberT-Tung22-Mar-05 19:31 
GeneralNeed Client Application PinmemberPrasadDashputre15-Feb-05 23:40 
GeneralRe: Need Client Application PinmemberT-Tung22-Mar-05 19:32 
GeneralRe: Need Client Application Pinmemberelectric00926-May-09 22:24 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 31 Jan 2005
Article Copyright 2005 by venura c.p.w. goonatillake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid