Click here to Skip to main content
11,428,825 members (62,296 online)
Click here to Skip to main content

Client Server Communication

, 17 Jan 2006
Rate this:
Please Sign up or sign in to vote.
This article gives information on client server communication using sockets.

Introduction

This article gives information on client server communication using sockets. Thanks to Ernest Laurentin for writing such an excellent class called SocketComm which provides methods which are useful in developing client-server applications.

Overview

Figure I-1 shows one of the programming frameworks. The left block represents the client application. The rest represents the server application. If the client application wants to send/receive data to/from a server, the client needs to create a socket (client socket) and use this socket to communicate with the server. At the server side, there are two kinds of sockets. One is the listening socket, which is responsible to take care of requests from new clients. The other is the service socket whose job is to taking care of all the communication between existing clients and the server. Once a new user requests to join the service, the listening socket will create a new service socket for this new user. The functionality of the service socket is like that of a representative of each client. In other words, if there are N users in this system, there will be N service sockets in the server side.

Overview of Server Application

The server will set up the service first. It will keep waiting any request from a new user. If a new request is granted, the server will create a new service socket to serve this new user.

Code for Listening Request

DWORD WINAPI CallServerThread(LPVOID lpPtr)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState())
   CSampleServerDlg* pThis = reinterpret_cast( lpPtr );
   _ASSERTE( pThis != NULL );
   SOCKET Soc;
   while(TRUE)
   {
    TCHAR szBuf[MAX_PATH] = {0};
    Soc = (SOCKET)pThis->m_hComm;
    fd_set fdRead = { 0 };
    Soc = accept(Soc,0,0);
    DWORD dWW = GetLastError();
    if(Soc != INVALID_SOCKET)
    {
      CString strTotClients = "";
      pThis->m_hNewSocHandle = (HANDLE) Soc;
      pThis->UpdateUserInfo();
    }     Sleep(500);
   }
   return 0;
}

Code for Service Socket

DWORD WINAPI ListeningThread(LPVOID lpPtr)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState())
   TCHAR szBuf[MAX_PATH] = {0};
   CListeningClass *pThis = reinterpret_cast(lpPtr);
   SOCKET Soc ;
   Soc = (SOCKET)pThis->m_hComm;
   if(Soc != INVALID_SOCKET)
   {
     while(TRUE)
     {
     int nRet = recv(Soc,szBuf,sizeof(szBuf),0);
     if(nRet != SOCKET_ERROR )
     {
     if(!::IsWindowVisible(pThis->m_hWnd))
     pThis->ShowWindow(SW_SHOW);
          CString strText = szBuf;
     pThis->m_Recv.SetWindowText(strText);
     }
     Sleep(100);
     }
   }
   return 0;
}

Code for Terminating Thread at the End

void CListeningClass::TerminateThread()
{
   DWORD dwExitCode = 0;
   while(TRUE)
   {
   ::TerminateThread(m_handle,dwExitCode);
    if(::GetExitCodeThread(m_handle,&dwExitCode))
    {
    if (dwExitCode !=STILL_ACTIVE)
    { 
    break;
    }
    else
     Sleep(10);
    }
  }
}

How the Demo Works

  1. Invoke the server application and click on “Start Server”. Now, the application starts listening to clients.
  2. Invoke the client application, specify the IP address, and click on “Connect”.

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

vamsidhar

United States United States
No Biography provided

Comments and Discussions

 
QuestionWhy client still is in list if clients is out of order ? Pin
SKJ15-Feb-11 22:05
memberSKJ15-Feb-11 22:05 
GeneralMy vote of 5 Pin
ankit jagga11-Aug-10 3:52
memberankit jagga11-Aug-10 3:52 
GeneralQuestion Pin
nazgulya5-Apr-10 20:12
membernazgulya5-Apr-10 20:12 
GeneralHi There Developer Pin
Sourabh Tomar15-Mar-08 23:18
memberSourabh Tomar15-Mar-08 23:18 
Generalport error Pin
gevorik11-Mar-07 23:59
membergevorik11-Mar-07 23:59 
GeneralRe: port error Pin
NitaJou2-Apr-07 18:24
memberNitaJou2-Apr-07 18:24 
GeneralRe: port error Pin
khanhkhi15-Oct-07 4:29
memberkhanhkhi15-Oct-07 4:29 

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 | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 17 Jan 2006
Article Copyright 2006 by vamsidhar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid