Click here to Skip to main content
12,701,125 members (29,307 online)
Click here to Skip to main content
Add your own
alternative version


67 bookmarked

Single Server, Multiple Clients - Win32/MFC classes for client/server communication with Unicode support

, 26 Jan 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
An implementation of Liyang Yu's classes for client/server communication for both Win32 and MFC, with Unicode and ANSI support.


This article presents a set of classes that are useful for client/server communication. These classes can be used in Win32 or MFC apps, and include Unicode support.

This article is based on Liyang Yu's excellent classes, which can be found here.

Porting to MFC and Unicode

Yu's original classes already supported Win32, but not Unicode. To be able to use these classes in both MFC and Win32 apps - either Unicode or ANSI - meant that all char variables and parameters had to be examined, and where necessary, converted to either TCHAR variables or LPCTSTR parameters. Of course, corresponding changes were required for string functions - e.g., converting strlen to _tcslen, etc.

Not all char variables were converted. Some functions still require use of char, because Winsock API is based on ANSI. Example: setsockopt.

Class Overview

There are five main classes:
  • ssmcException - captures and reports errors.
  • ssmcHostInfo - if you pass a host name to constructor, this class will tell you IP address, and if you pass in an IP address, it will tell you host name.
  • ssmcSocket - this class encapsulates socket-related API calls into a generic socket class. The derived class ssmcTcpSocket wraps TCP-specific functionality.
  • ssmcThread - this is a generic class that includes APIs to create and execute a thread.
  • ssmcThreadData - main purpose of this class is to encapsulate data that thread needs. The derived classes ssmcClientThreadData and ssmcServerThreadData wrap data needed by server's client and server threads.

Concepts and Facilities

Yu has already described classes in his article, so I will focus here on how to use these classes in sample apps.

The data and control flow of socket-based systems is shown in following figure:

In client and server demo apps, messages are null-terminated strings. However, any type of data, including binary data, may be sent by these classes; of course, both client and server must understand data format.

In demo apps, client prepends each message with its thread ID. The server app expects this thread ID to be present, and removes it before echoing message back to client.

Server App

In Startup() function, following steps are performed:
  1. Initialize Winsock library:
  2. Create server socket on local host:
    m_pServer = new ssmcTcpSocket(PORTNUM);
  3. Create a server thread to implement server processing: listen to socket, accept client calls and communicate with clients. This will free the main thread to do other processing.
    m_pServerData = new ssmcServerThreadData(m_pServer, serverName);
    ssmcThread* pServerThread = new ssmcThread(ServerThread, (void*)m_pServerData);
    ServerThread() processing is diagrammed in following figure:

    In server thread, a new client thread is created each time server accepts a new client. The client thread processing is diagrammed in following figure:

  4. The final thing Startup() function does is start a timer that will check for active clients and display count on server dialog.
The above steps are diagrammed in following figure:

Now that server thread has started, server is ready to accept new client connections.

The server app initially looks like this:

Client App

When user clicks Connect button, client app's StartClient() function is called:

The Send button is now enabled. When it's clicked, message is sent to server, which will echo it back to client:

After user connects and sends a message, client app looks like this:

and server app looks like:

Detecting Client Disconnect

A client might disconnect from server programmatically or because a program fault causes client app to exit. For either case, server's ClientThread() function will detect disconnect, set bClientIsFinished flag to true, and exit thread.

When it detects a client disconnect, server app looks like:

Detecting Server Disconnect

Unlike server app, client app is usually not doing a recv, and so detecting when server disconnects involves some extra work. In client app, this involves implementing a "ping" facility, where the client periodically sends a "ping" message to server.

When it detects a server disconnect, client app looks like:

Notice that ping checkbox is checked.

Sending Multiple Messages

The client app has been set up to allow more than one message to be sent. Each message includes a message number, so echoed message can be verified.

ANSI Clients and Unicode Clients

The demo project has both ANSI and Unicode build configurations. Regardless of whether server is ANSI or Unicode, it will correctly read and send messages. This works because a Unicode client prepends a Unicode header (Byte Order Mark, or BOM) to each message it sends. The server sees this BOM, and 1) interprets the message text as Unicode; and 2) sends back a Unicode message, with BOM. Obviously, this works fine for demo, because messages are always text strings.

Using BOM technique, a Unicode server can handle both ANSI and Unicode clients; and so can an ANSI server (try it and see).

More Info...
According to Unicode Consortium (see UTF & BOM), signature at beginning of certain data streams (such as unmarked plaintext files). is referred to as the BOM character, for Byte Order Mark. For little-endian systems such as those based on Intel x86 processors, the BOM is encoded as the two byte sequence FF FE (hex values). For big-endian systems, the BOM is FE FF. Where a BOM is useful is with files that are typed as text, but not known to be in either big- or little-endian format. In that situation, BOM serves both as a hint that the text is Unicode, and also what type of byte-ordering was used to create the text.

How To Use

To integrate ssmc classes into your server app, you need to add following files to your project:

  • ssmcException.cpp
  • ssmcException.h
  • ssmcHostInfo.cpp
  • ssmcHostInfo.h
  • ssmcSocket.cpp
  • ssmcSocket.h
  • ssmcThread.cpp
  • ssmcThread.h
  • ssmcThreadData.cpp
  • ssmcThreadData.h
  • XWSAError.h
To integrate ssmc classes into your client app, you need to add following files to your project:
  • ssmcException.cpp
  • ssmcException.h
  • ssmcHostInfo.cpp
  • ssmcHostInfo.h
  • ssmcSocket.cpp
  • ssmcSocket.h
  • XWSAError.h

Both client and server need to be linked to XWSAError.lib, and XWSAError.dll needs to be in exe's directory when app is run.

For details on how to use ssmc classes, refer to code in mfc clientDlg.cpp and mfc serverDlg.cpp.


Revision History

Version 1.0 - 2005 January 26

  • Initial public release


This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


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


About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see

You may also be interested in...

Comments and Discussions

QuestionRemove netbios lookup? Pin
dittotulsa10-Jul-12 8:11
memberdittotulsa10-Jul-12 8:11 
AnswerRe: Remove netbios lookup? Pin
dittotulsa10-Jul-12 8:39
memberdittotulsa10-Jul-12 8:39 
QuestionCan't build in Visual C++ 2008 Pin
ddas-edEn19-Oct-11 2:44
memberddas-edEn19-Oct-11 2:44 
SuggestionRe: Can't build in Visual C++ 2008 Pin
ddas-edEn20-Oct-11 23:24
memberddas-edEn20-Oct-11 23:24 
SuggestionRe: Can't build in Visual C++ 2008 Pin
ddas-edEn20-Oct-11 23:48
memberddas-edEn20-Oct-11 23:48 
GeneralMy vote of 5 Pin
iliyas.patel24-Feb-11 2:55
memberiliyas.patel24-Feb-11 2:55 
AnswerRe: My vote of 5 Pin
Hans Dietrich28-Apr-11 22:36
mentorHans Dietrich28-Apr-11 22:36 
QuestionGood demo but does not compile using VS2005 Pin
andyytang2-Nov-07 21:48
memberandyytang2-Nov-07 21:48 
QuestionUnicode support? Pin
nomicism26-Oct-07 7:40
membernomicism26-Oct-07 7:40 
Generalmemory leaks! Pin
rain690427-Oct-06 20:34
memberrain690427-Oct-06 20:34 
Generalaccept Error,when have a route in the network Pin
xipeng13-Aug-06 22:14
memberxipeng13-Aug-06 22:14 
QuestionHow To check the CD Writter,Printer, Download Size Status such as How Copies writted and printed Pin
alexmcada3-Oct-05 2:37
memberalexmcada3-Oct-05 2:37 
Questionhow to set keep-alive-time Pin
hanxianbin28-May-05 17:30
memberhanxianbin28-May-05 17:30 
Generalproblems.... Pin
anybody anyone29-Mar-05 3:36
memberanybody anyone29-Mar-05 3:36 
QuestionWhy ? Pin
GuimaSun23-Mar-05 14:54
memberGuimaSun23-Mar-05 14:54 
AnswerRe: Why ? Pin
Hans Dietrich28-Apr-11 22:35
mentorHans Dietrich28-Apr-11 22:35 
QuestionServer software preformance ??? Pin
dchyla6-Feb-05 2:08
memberdchyla6-Feb-05 2:08 
AnswerRe: Server software preformance ??? Pin
Rocom.liu22-Feb-05 23:54
memberRocom.liu22-Feb-05 23:54 
AnswerRe: Server software preformance ??? Pin
Rocom.liu23-Feb-05 0:05
memberRocom.liu23-Feb-05 0:05 
GeneralRe: Server software preformance ??? Pin
dchyla26-Feb-05 0:11
memberdchyla26-Feb-05 0:11 
GeneralRe: Server software preformance ??? Pin
Avis p1-Aug-05 4:05
memberAvis p1-Aug-05 4:05 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 26 Jan 2005
Article Copyright 2005 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid