Click here to Skip to main content
15,896,111 members
Articles / Desktop Programming / MFC
Article

Network Development Kit 2.0

Rate me:
Please Sign up or sign in to vote.
4.88/5 (114 votes)
29 Dec 2006CPOL 2.9M   25.7K   496   601
Network Development Kit is a set of simple classes for a client-server architecture.

NDK Standard

NDK in an Extension DLL

Introduction

With the success of NDK 1.0, I decided to improve the functionality. NDK represents Network Development Kit. The NDK is a set of classes that implements a client/server architecture. The NDK hides all the complexity of the connection, sending, and receiving of data over a network. You only have to deal with three classes: CNDKServer, CNDKClient, and CNDKMessage. With just a few methods to override, you obtain a complete robust client/server application. The NDK is based on the class CSocket from MFC, so you can run your application on a local network or on the Internet without any change. To easily understand the integration of the NDK in an application, you'll find at the end of this article a complete chat application.

Classes

CNDKServer: Server side of the client/server architecture

Attributes:

  • BOOL IsStarted() const;
  • long GetPort() const;
  • long GetNbUsers() const;
  • void GetUserIds(CLongArray& alIds) const;

Operations:

  • BOOL StartListening(long lPort);
  • void Stop();
  • BOOL SendMessageToUser(long lUserId, CNDKMessage& message);
  • BOOL SendMessageToAllUsers(CNDKMessage& message);
  • BOOL SendMessageToSomeUsers(const CLongArray& alUserIds, CNDKMessage& message);
  • BOOL SendMessageToAllUsersExceptFor(long lUserId, CNDKMessage& message);
  • BOOL SendMessageToAllUsersExceptFor(const CLongArray& alUserIds, CNDKMessage& message);
  • BOOL DisconnectUser(long lUserId);
  • void DisconnectAllUsers();
  • BOOL PingUser(long lUserId);
  • void PingAllUsers();

Callbacks:

  • virtual BOOL OnIsConnectionAccepted() = 0;
  • virtual void OnConnect(long lUserId) = 0;
  • virtual void OnMessage(long lUserId, CNDKMessage& message) = 0;
  • virtual void OnDisconnect(long lUserId, NDKServerDisconnection disconnectionType) = 0;
  • virtual void OnPing(long lUserId, long lNbMilliseconds);

CNDKClient: Client side of the client/server architecture

Attributes:

  • BOOL IsConnected() const;
  • BOOL GetIpAndPort(CString& strIp, long& lPort) const;

Operations:

  • BOOL OpenConnection(const CString& strServerIp, long lPort);
  • void CloseConnection();
  • BOOL SendMessageToServer(CNDKMessage& message);
  • BOOL PingServer();

Callbacks:

  • virtual void OnMessage(CNDKMessage& message) = 0;
  • virtual void OnDisconnect(NDKClientDisconnection disconnectionType) = 0;
  • virtual void OnPing(long lNbMilliseconds);

CNDKMessage: Encapsulation of the data that is sent and received by CNDKServer and CNDKClient

Attributes:

  • void SetId(long lId);
  • long GetId() const;
  • int GetNbElements() const;

Operations:

  • void Add(TYPE typeData);
  • void SetAt(long lIndex, TYPE typeData);
  • void GetAt(long lIndex, TYPE& typeData) const;
  • void GetNext(TYPE& typeData);

Where TYPE can be a UCHAR, char, USHORT, short, UINT, int, long, float, double, CString, or LPVOID data.

What's new in 2.0

  • Hungarian notation is used everywhere
  • CNDKServer notifies automatically CNDKClient when it disconnects and vice-versa
  • CNDKServer has new methods to send a message to a group of users
  • CNDKMessage is easier to use and more robust
  • New methods to ping the server or the client
  • NDK prefix added to all classes to make sure that all class names are unique in your project
  • NDK is standalone or encapsulated in an extension DLL

Client/Server chat

Here are two screenshots of the chat application. Most features of the NDK are shown in both programs.

Client Image

Server Image

Sample code from the chat client

void CChatClientDlg::OnButtonSend() 
{
   if (UpdateData(TRUE))
   {
      // Create the message with a unique Id: ChatText
      CNDKMessage message(ChatText);

      // Add the data into the message
      message.Add(m_strChatInput);

      // Send the message to the server      
      SendMessageToServer(message);

      // Update the UI of the dialog
      AddText(m_strChatInput);

      m_strChatInput.Empty();
      UpdateData(FALSE);
   }
}

Sample code from the chat server

void CChatServerDlg::OnMessage(long lUserId, CNDKMessage& message)
{
   switch (message.GetId())
   {
      //...

      case ChatText:
      {
         CString strNickname;

         // Obtain the nickname stored in the map
         m_mapIdsNicknames.Lookup(lUserId, strNickname);

         CString strText;

         // Get the text encapsulated in the message
         message.GetAt(0, strText);

         // Update the UI of the dialog
         AddText(strNickname + _T(": ") + strText);

         // Prepare the message
         message.SetAt(0, strNickname);
         message.SetAt(1, strText);

         // Send the message to all others users
         SendMessageToAllUsersExceptFor(lUserId, message);
      }
      break;

      //...
   }
}

History

  • 17 December 2006
    • Fixed a bug in NDKMessage concerning the length parameter of the GetAt and GetNext methods.
  • 14 October 2005
    • Telnet connections are now disconnected when a connection sends data to the server. Thanks to Stephan Douglas for the solution.
    • Fixed a bug that resolves socket notification since the NDK uses Visual C++ .NET. After a short time, socket notifications were not send anymore.
    • A verification is now made in the serialization of a CNDKMessage to make sure that the message is really a CNDKMessage.
    • Fixed a bug in the method SendMessageToSomeUsers in the NDKServer.cpp.
    • The project is now converted for Visual C++ .NET.

Fact

In 2002, the NDK was used in two NASA experiments for the Endeavor mission.

Conclusion

You don't have to be a guru of networking to create a client/server application, you only need to understand the above classes.

If you use the NDK in your project, I would really appreciate to receive an E-mail from you. I plan to make a web page of all programs using the NDK.

I would like to thank Yannick Létourneau who helped me in NDK 1.0.

Good Programming!

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) Mirego
Canada Canada
My name is Sébastien Lachance.

I love C# developing Windows Phone and Windows 8 applications.

When I’m not in front of a computer, my hobbies include playing bridge, poker and other card games, biking, reading technology news.

Comments and Discussions

 
GeneralRe: Fixed Pin
Shutter27-Jun-03 12:23
Shutter27-Jun-03 12:23 
GeneralNot Fixed Pin
Andrius Mudinas17-Jul-03 11:22
Andrius Mudinas17-Jul-03 11:22 
GeneralRe: Not Fixed Pin
Djof18-Jul-03 7:26
Djof18-Jul-03 7:26 
GeneralRe: Not Fixed Pin
ArchieCoder9-Feb-04 10:15
ArchieCoder9-Feb-04 10:15 
GeneralRe: Client stops receiving messages Pin
Anonymous15-Jun-03 23:11
Anonymous15-Jun-03 23:11 
Generalprivate msg between clients Pin
mak4ever2-May-03 5:29
mak4ever2-May-03 5:29 
GeneralInfo on using as a Service Pin
m_roadster2929-Apr-03 4:34
m_roadster2929-Apr-03 4:34 
GeneralRe: Info on using as a Service Pin
speedpacer4-Jul-03 7:55
speedpacer4-Jul-03 7:55 
I don't know if you've already found a solution for this or not, but I'm using Xiangyang Liu's "XYNTService" class found here on Codeproject:

http://www.codeproject.com/system/xyntservice.asp

Additionally, I'm using Nishant S's "CServiceHelper" class to stop the service in my OnClose function:

http://www.codeproject.com/system/cservicehelper.asp

They both did a really good job on these classes and it's working great for me, although it does consist of an additional executable, XYNTService.exe, and an ini file, XYNTService.ini, unless you rename them of course.

One good thing about it is that you can add a "PauseEnd" statement in the ini file to stop the server, which will give you time to disconnect all of your users before it shuts down. What this will do is send the WM_CLOSE message to the server, then pause for the specified time period before sending the TerminateProcess() message.

The server won't necessarily be running "as a service" but it works in the same fashion. My understanding is that it's pretty tricky, if at all possible, to run an MFC-based application as a service. I tried to do the same thing and was unsuccessful. If anyone else knows of a way, please cough it up! Poke tongue | ;-P

Good luck!

GeneralDisconnecting crashed client Pin
HR coder22-Apr-03 12:59
HR coder22-Apr-03 12:59 
GeneralRe: Disconnecting crashed client Pin
ArchieCoder29-Apr-03 8:15
ArchieCoder29-Apr-03 8:15 
GeneralRe: Disconnecting crashed client Pin
aparra9-May-03 5:20
aparra9-May-03 5:20 
GeneralRe: Disconnecting crashed client Pin
ArchieCoder12-May-03 8:44
ArchieCoder12-May-03 8:44 
GeneralRe: Disconnecting crashed client - my way Pin
HR coder14-Jun-03 20:31
HR coder14-Jun-03 20:31 
QuestionHow can I combine NDK2 with multithread(thread pool)? Pin
Gary Liu7-Apr-03 22:03
Gary Liu7-Apr-03 22:03 
AnswerRe: How can I combine NDK2 with multithread(thread pool)? Pin
ArchieCoder8-Apr-03 4:20
ArchieCoder8-Apr-03 4:20 
GeneralWindows Freezing Pin
Anonymous27-Mar-03 2:26
Anonymous27-Mar-03 2:26 
GeneralRe: Windows Freezing Pin
ArchieCoder27-Mar-03 3:45
ArchieCoder27-Mar-03 3:45 
GeneralHelpful Hints for 802.11b WinCE/PocketPC Devices Pin
Like2Byte21-Mar-03 5:10
Like2Byte21-Mar-03 5:10 
GeneralRe: Helpful Hints for 802.11b WinCE/PocketPC Devices Pin
LoveMagic2-Apr-03 20:11
LoveMagic2-Apr-03 20:11 
GeneralRe: Helpful Hints for 802.11b WinCE/PocketPC Devices Pin
ArchieCoder3-Apr-03 4:03
ArchieCoder3-Apr-03 4:03 
GeneralRe: Helpful Hints for 802.11b WinCE/PocketPC Devices Pin
Like2Byte10-Apr-03 7:05
Like2Byte10-Apr-03 7:05 
GeneralTRY & CATCH Block errors Pin
Like2Byte20-Mar-03 3:24
Like2Byte20-Mar-03 3:24 
GeneralRe: TRY & CATCH Block errors Pin
ArchieCoder20-Mar-03 3:44
ArchieCoder20-Mar-03 3:44 
GeneralRe: TRY & CATCH Block errors Pin
Like2Byte20-Mar-03 3:59
Like2Byte20-Mar-03 3:59 
GeneralRe: TRY & CATCH Block errors Pin
ArchieCoder20-Mar-03 4:05
ArchieCoder20-Mar-03 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.