Click here to Skip to main content
15,884,177 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
4.58/5 (24 votes)
26 Jan 2005CPOL6 min read 151.5K   4.6K   71   21
An implementation of Liyang Yu's classes for client/server communication for both Win32 and MFC, with Unicode and ANSI support.

Introduction

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:

Image 1

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:
    ssmcTcpSocket::initialize();
    
  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);
    m_pServerData->setThreadHandle(pServerThread);
    pServerThread->execute();
    
    ServerThread() processing is diagrammed in following figure:

    Image 2

    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:

    Image 3

  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:

Image 4

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

The server app initially looks like this:

Image 5

Client App

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

Image 6

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

Image 7

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

Image 8

and server app looks like:

Image 9

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:

Image 10

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:

Image 11

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.

Image 12

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.

Acknowledgments

Revision History

Version 1.0 - 2005 January 26

  • Initial public release

Usage

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.



License

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


Written By
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 www.hdsoft.org.






Comments and Discussions

 
QuestionRemove netbios lookup? Pin
dittotulsa10-Jul-12 7:11
dittotulsa10-Jul-12 7:11 
AnswerRe: Remove netbios lookup? Pin
dittotulsa10-Jul-12 7:39
dittotulsa10-Jul-12 7:39 
QuestionCan't build in Visual C++ 2008 Pin
ddas-edEn19-Oct-11 1:44
ddas-edEn19-Oct-11 1:44 
SuggestionRe: Can't build in Visual C++ 2008 Pin
ddas-edEn20-Oct-11 22:24
ddas-edEn20-Oct-11 22:24 
SuggestionRe: Can't build in Visual C++ 2008 Pin
ddas-edEn20-Oct-11 22:48
ddas-edEn20-Oct-11 22:48 
GeneralMy vote of 5 Pin
iliyas.patel24-Feb-11 1:55
iliyas.patel24-Feb-11 1:55 
AnswerRe: My vote of 5 Pin
Hans Dietrich28-Apr-11 21:36
mentorHans Dietrich28-Apr-11 21:36 
QuestionGood demo but does not compile using VS2005 Pin
andyytang2-Nov-07 20:48
andyytang2-Nov-07 20:48 
QuestionUnicode support? Pin
nomicism26-Oct-07 6:40
nomicism26-Oct-07 6:40 
Generalmemory leaks! Pin
rain690427-Oct-06 19:34
rain690427-Oct-06 19:34 
Generalaccept Error,when have a route in the network Pin
xipeng13-Aug-06 21:14
xipeng13-Aug-06 21:14 
QuestionHow To check the CD Writter,Printer, Download Size Status such as How Copies writted and printed Pin
Member 23255383-Oct-05 1:37
Member 23255383-Oct-05 1:37 
Questionhow to set keep-alive-time Pin
hanxianbin28-May-05 16:30
hanxianbin28-May-05 16:30 
Generalproblems.... Pin
anybody anyone29-Mar-05 2:36
anybody anyone29-Mar-05 2:36 
QuestionWhy ? Pin
GuimaSun23-Mar-05 13:54
GuimaSun23-Mar-05 13:54 
AnswerRe: Why ? Pin
Hans Dietrich28-Apr-11 21:35
mentorHans Dietrich28-Apr-11 21:35 
QuestionServer software preformance ??? Pin
dchyla6-Feb-05 1:08
dchyla6-Feb-05 1:08 
AnswerRe: Server software preformance ??? Pin
Rocom.liu22-Feb-05 22:54
Rocom.liu22-Feb-05 22:54 
AnswerRe: Server software preformance ??? Pin
Rocom.liu22-Feb-05 23:05
Rocom.liu22-Feb-05 23:05 
GeneralRe: Server software preformance ??? Pin
dchyla25-Feb-05 23:11
dchyla25-Feb-05 23:11 
GeneralRe: Server software preformance ??? Pin
Avis p1-Aug-05 3:05
Avis p1-Aug-05 3: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.