Click here to Skip to main content
13,353,706 members (55,450 online)
Click here to Skip to main content
Add your own
alternative version


492 bookmarked
Posted 15 Aug 2000

Network Development Kit 2.0

, 29 Dec 2006
Rate this:
Please Sign up or sign in to vote.
Network Development Kit is a set of simple classes for a client-server architecture.

NDK Standard

NDK in an Extension DLL


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.


CNDKServer: Server side of the client/server architecture


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


  • 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();


  • 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


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


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


  • 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


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


  • 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

      // Send the message to the server      

      // Update the UI of the dialog


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);



  • 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.


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


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!


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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralRe: Reason of Crash: There is a logical problem with CNDKServerSocket::ReceiveMessage which will lead to the code being blocked. Pin
Sebastien_Lachance19-Dec-11 4:08
memberSebastien_Lachance19-Dec-11 4:08 
QuestionSSL with NDK Pin
Nacereddine23-May-09 1:17
memberNacereddine23-May-09 1:17 
AnswerRe: SSL with NDK Pin
Sebastien_Lachance23-May-09 5:26
memberSebastien_Lachance23-May-09 5:26 
QuestionServer / Client Disconnect Pin
xbarf8-Mar-09 23:35
memberxbarf8-Mar-09 23:35 
AnswerRe: Server / Client Disconnect Pin
Sebastien_Lachance9-Mar-09 9:33
memberSebastien_Lachance9-Mar-09 9:33 
GeneralMy vote of 5 Pin
Rajesh R Subramanian23-Jan-09 19:59
mvpRajesh R Subramanian23-Jan-09 19:59 
GeneralRe: My vote of 5 Pin
Sebastien_Lachance25-Jan-09 4:53
memberSebastien_Lachance25-Jan-09 4:53 
QuestionServer Side Oftenly Error Pin
chrisliando16-Dec-08 14:50
memberchrisliando16-Dec-08 14:50 
Sebastien, I have implemented the NDK 2.0 class you've developed on a small client server connection with about 10 clients connected to the server. Only 2 clients run on Windows Vista Business and the rest are still using Windows XP Professional SP2.

My problem:
1. Why does the server oftenly experiencing Runtime error? The error is something like the usual application error which then show window Send or Don't Send to Microsoft. After the error, I must restart the server application and it is OK. But after some time, the error happens again.
This is very annoying because this connection is really important. And what makes it a serious problem is that the error can be happen even 2-3 times a week on 5 days office work! And I still don't know what cause it.

What should I look up to find the cause? Does the number of clients connected has any relation with this error?

2. I wonder to make this mechanism:
If the server error, then all clients will surely be disconnected from the server. When I RESTART the server application, is it possible to AUTOMATICALLY re-connect all the clients WITHOUT the client has to RESTARTING their application?

I am looking forward to hear from you soon.

Thank you very much.
AnswerRe: Server Side Oftenly Error Pin
Sebastien_Lachance17-Dec-08 17:04
memberSebastien_Lachance17-Dec-08 17:04 
QuestionRe: Server Side Oftenly Error Pin
chrisliando17-Dec-08 17:24
memberchrisliando17-Dec-08 17:24 
AnswerRe: Server Side Oftenly Error Pin
Sebastien_Lachance17-Dec-08 17:35
memberSebastien_Lachance17-Dec-08 17:35 
QuestionRe: Server Side Oftenly Error Pin
chrisliando17-Dec-08 18:04
memberchrisliando17-Dec-08 18:04 
AnswerRe: Server Side Oftenly Error Pin
Sebastien_Lachance18-Dec-08 6:20
memberSebastien_Lachance18-Dec-08 6:20 
QuestionRe: Server Side Oftenly Error Pin
chrisliando18-Dec-08 13:53
memberchrisliando18-Dec-08 13:53 
AnswerRe: Server Side Oftenly Error Pin
Sebastien_Lachance19-Dec-08 5:50
memberSebastien_Lachance19-Dec-08 5:50 
QuestionRe: Server Side Oftenly Error [modified] Pin
chrisliando19-Dec-08 21:33
memberchrisliando19-Dec-08 21:33 
QuestionGreat code, but client seems to go to sleep in my application Pin
RWB24-Nov-08 4:58
memberRWB24-Nov-08 4:58 
GeneralTry to combile in VS2005 Pin
viper773-Nov-08 6:55
memberviper773-Nov-08 6:55 
GeneralRe: Try to combile in VS2005 Pin
wTano10-Nov-08 3:05
memberwTano10-Nov-08 3:05 
GeneralNice job! Pin
zhglzh15-Oct-08 2:02
memberzhglzh15-Oct-08 2:02 
GeneralRe: Nice job! Pin
Sebastien_Lachance15-Oct-08 2:52
memberSebastien_Lachance15-Oct-08 2:52 
QuestionCan I use NDK using IOCP? Pin
gwenchill30-Sep-08 18:12
membergwenchill30-Sep-08 18:12 
AnswerRe: Can I use NDK using IOCP? Pin
Sebastien_Lachance1-Oct-08 3:16
memberSebastien_Lachance1-Oct-08 3:16 
GeneralRe: Can I use NDK using IOCP? Pin
gwenchill2-Oct-08 8:08
membergwenchill2-Oct-08 8:08 
GeneralRe: Can I use NDK using IOCP? Pin
Sebastien_Lachance3-Oct-08 16:22
memberSebastien_Lachance3-Oct-08 16:22 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 29 Dec 2006
Article Copyright 2000 by ArchieCoder
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid