Click here to Skip to main content
15,881,803 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   495   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

 
QuestionCan I use this kit with vb.net 2005? Pin
MAP Tiger20-Feb-07 22:42
MAP Tiger20-Feb-07 22:42 
QuestionHow can to send an Object as data from client to server Pin
Vis_liner18-Feb-07 9:33
Vis_liner18-Feb-07 9:33 
AnswerRe: How can to send an Object as data from client to server Pin
ArchieCoder20-Feb-07 14:49
ArchieCoder20-Feb-07 14:49 
Questionis possible to use NDK for send other files Pin
metal aucard8-Feb-07 20:34
metal aucard8-Feb-07 20:34 
AnswerRe: is possible to use NDK for send other files Pin
ArchieCoder9-Feb-07 6:17
ArchieCoder9-Feb-07 6:17 
GeneralFantastic but I found some Problems Pin
The Yariv2-Jan-07 2:52
The Yariv2-Jan-07 2:52 
GeneralRe: Fantastic but I found some Problems Pin
ArchieCoder2-Jan-07 10:50
ArchieCoder2-Jan-07 10:50 
GeneralRe: Fantastic but I found some Problems Pin
The Yariv2-Jan-07 21:47
The Yariv2-Jan-07 21:47 
there are also memory leaks , can you check this?

NDK.DLL Terminating!<br />
Detected memory leaks!<br />
Dumping objects -><br />
{21885} normal block at 0x0760F580, 260 bytes long.<br />
 Data: <NotifyBlockProgr> 4E 6F 74 69 66 79 42 6C 6F 63 6B 50 72 6F 67 72 <br />
{21881} normal block at 0x075D57C0, 260 bytes long.<br />
 Data: <NotifyBlockProgr> 4E 6F 74 69 66 79 42 6C 6F 63 6B 50 72 6F 67 72 <br />
{21877} normal block at 0x07611C40, 260 bytes long.<br />
 Data: <NotifyBlockProgr> 4E 6F 74 69 66 79 42 6C 6F 63 6B 50 72 6F 67 72 <br />
{15095} normal block at 0x0760F2E0, 20 bytes long.<br />
 Data: <X F o r m   O u > 58 00 46 00 6F 00 72 00 6D 00 20 00 4F 00 75 00 <br />
{15094} normal block at 0x0760F290, 16 bytes long.<br />
 Data: <$             ` > 24 1C 94 09 00 00 00 00 FF 01 00 00 00 F1 60 07 <br />
{15093} normal block at 0x0760F1A0, 176 bytes long.<br />
 Data: <D$        `     > 44 24 94 09 FF 01 00 00 A0 F1 60 07 00 00 00 00 <br />
{15092} normal block at 0x0760F150, 18 bytes long.<br />
 Data: <X F o r m   I n > 58 00 46 00 6F 00 72 00 6D 00 20 00 49 00 6E 00 <br />
{15091} normal block at 0x0760F100, 16 bytes long.<br />
 Data: <              a > DC 1B 94 09 00 00 00 00 FE 01 00 00 10 0D 61 07 <br />
{15090} normal block at 0x0760EFD8, 232 bytes long.<br />
 Data: < #        `     > 9C 23 94 09 FE 01 00 00 D8 EF 60 07 00 00 00 00 <br />
{15086} normal block at 0x07611FD8, 16 bytes long.<br />
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <br />
{15084} normal block at 0x07610D10, 16 bytes long.<br />
 Data: <             ?a > 1C 14 94 09 00 00 00 00 FB 01 00 00 10 3F 61 07 <br />
{15083} normal block at 0x0760EDE8, 232 bytes long.<br />
 Data: <          `     > B4 13 94 09 FB 01 00 00 E8 ED 60 07 01 00 00 00 <br />
{15067} normal block at 0x075D52D8, 36 bytes long.<br />
 Data: <                > EA 03 00 00 00 04 00 00 00 05 00 00 00 00 00 00 <br />
{14500} normal block at 0x076108B8, 20 bytes long.<br />
 Data: <X F o r m   O u > 58 00 46 00 6F 00 72 00 6D 00 20 00 4F 00 75 00 <br />
{14499} normal block at 0x07613F10, 16 bytes long.<br />
 Data: <$             a > 24 1C 94 09 00 00 00 00 8C 01 00 00 08 1F 61 07 <br />
{14498} normal block at 0x07614F08, 176 bytes long.<br />
 Data: <D$       Oa     > 44 24 94 09 8C 01 00 00 08 4F 61 07 00 00 00 00 <br />
{14497} normal block at 0x07613EC0, 18 bytes long.<br />
 Data: <X F o r m   I n > 58 00 46 00 6F 00 72 00 6D 00 20 00 49 00 6E 00 <br />
{14496} normal block at 0x07611F08, 16 bytes long.<br />
 Data: <             T] > DC 1B 94 09 00 00 00 00 8B 01 00 00 C8 54 5D 07 <br />
{14495} normal block at 0x07610790, 232 bytes long.<br />
 Data: < #        a     > 9C 23 94 09 8B 01 00 00 90 07 61 07 00 00 00 00 <br />
{14491} normal block at 0x075DB7F0, 16 bytes long.<br />
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <br />
{14489} normal block at 0x075D54C8, 16 bytes long.<br />
 Data: <             %a > 1C 14 94 09 00 00 00 00 88 01 00 00 20 25 61 07 <br />
{14488} normal block at 0x07611D90, 232 bytes long.<br />
 Data: <          a     > B4 13 94 09 88 01 00 00 90 1D 61 07 01 00 00 00 <br />
{14472} normal block at 0x076125C0, 36 bytes long.<br />
 Data: <                > EA 03 00 00 00 04 00 00 00 05 00 00 00 00 00 00 <br />
{2530} normal block at 0x07612570, 20 bytes long.<br />
 Data: <X F o r m   O u > 58 00 46 00 6F 00 72 00 6D 00 20 00 4F 00 75 00 <br />
{2529} normal block at 0x07612520, 16 bytes long.<br />
 Data: <$       y    #a > 24 1C 94 09 00 00 00 00 79 00 00 00 90 23 61 07 <br />
{2528} normal block at 0x07612430, 176 bytes long.<br />
 Data: <D$  y   0$a     > 44 24 94 09 79 00 00 00 30 24 61 07 00 00 00 00 <br />
{2527} normal block at 0x076123E0, 18 bytes long.<br />
 Data: <X F o r m   I n > 58 00 46 00 6F 00 72 00 6D 00 20 00 49 00 6E 00 <br />
{2526} normal block at 0x07612390, 16 bytes long.<br />
 Data: <        x   P!a > DC 1B 94 09 00 00 00 00 78 00 00 00 50 21 61 07 <br />
{2525} normal block at 0x07612268, 232 bytes long.<br />
 Data: < #  x   h"a     > 9C 23 94 09 78 00 00 00 68 22 61 07 00 00 00 00 <br />
{2521} normal block at 0x07611F88, 16 bytes long.<br />
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <br />
{2519} normal block at 0x07612150, 16 bytes long.<br />
 Data: <        u   P a > 1C 14 94 09 00 00 00 00 75 00 00 00 50 19 61 07 <br />
{2518} normal block at 0x07612028, 232 bytes long.<br />
 Data: <    u   ( a     > B4 13 94 09 75 00 00 00 28 20 61 07 01 00 00 00 <br />
{2502} normal block at 0x076115F8, 36 bytes long.<br />
 Data: <                > EA 03 00 00 00 04 00 00 00 05 00 00 00 00 00 00 <br />
{1935} normal block at 0x076119A0, 20 bytes long.<br />
 Data: <X F o r m   O u > 58 00 46 00 6F 00 72 00 6D 00 20 00 4F 00 75 00 <br />
{1934} normal block at 0x07611950, 16 bytes long.<br />
 Data: <$             a > 24 1C 94 09 00 00 00 00 06 00 00 00 C0 17 61 07 <br />
{1933} normal block at 0x07611860, 176 bytes long.<br />
 Data: <D$      ` a     > 44 24 94 09 06 00 00 00 60 18 61 07 00 00 00 00 <br />
{1932} normal block at 0x07611810, 18 bytes long.<br />
 Data: <X F o r m   I n > 58 00 46 00 6F 00 72 00 6D 00 20 00 49 00 6E 00 <br />
{1931} normal block at 0x076117C0, 16 bytes long.<br />
 Data: <              a > DC 1B 94 09 00 00 00 00 05 00 00 00 A8 15 61 07 <br />
{1930} normal block at 0x07611698, 232 bytes long.<br />
 Data: < #        a     > 9C 23 94 09 05 00 00 00 98 16 61 07 00 00 00 00 <br />
{1926} normal block at 0x075D5220, 16 bytes long.<br />
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <br />
{1924} normal block at 0x076115A8, 16 bytes long.<br />
 Data: <                > 1C 14 94 09 00 00 00 00 02 00 00 00 00 00 00 00 <br />
{1923} normal block at 0x07611480, 232 bytes long.<br />
 Data: <          a     > B4 13 94 09 02 00 00 00 80 14 61 07 01 00 00 00 <br />
{1907} normal block at 0x075D5918, 36 bytes long.<br />
 Data: <                > EA 03 00 00 00 04 00 00 00 05 00 00 00 00 00 00 <br />
f:\sp\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {924} normal block at 0x075DDD48, 42 bytes long.<br />
 Data: <, ?x            > 2C 08 3F 78 19 00 00 00 19 00 00 00 01 00 00 00 <br />
{533} normal block at 0x075C3800, 40 bytes long.<br />
 Data: <Lv;       \     > 4C 76 3B 06 CD CD CD CD 98 1D 5C 07 00 00 00 00 <br />
{526} normal block at 0x075C1E38, 1168 bytes long.<br />
 Data: <p               > 70 1E 00 00 00 00 00 00 01 00 00 00 02 00 00 00 <br />
Object dump complete.

GeneralRe: Fantastic but I found some Problems Pin
ArchieCoder3-Jan-07 2:44
ArchieCoder3-Jan-07 2:44 
GeneralRe: Fantastic but I found some Problems Pin
The Yariv3-Jan-07 20:23
The Yariv3-Jan-07 20:23 
GeneralRe: Fantastic but I found some Problems Pin
ArchieCoder4-Jan-07 5:08
ArchieCoder4-Jan-07 5:08 
GeneralFantastic but I found some Problems Pin
The Yariv2-Jan-07 2:50
The Yariv2-Jan-07 2:50 
GeneralAnother compile problem Pin
Joeny30-Dec-06 9:16
Joeny30-Dec-06 9:16 
GeneralRe: Another compile problem Pin
ArchieCoder30-Dec-06 17:52
ArchieCoder30-Dec-06 17:52 
GeneralRe: Another compile problem Pin
Steve Mann6-May-07 20:04
Steve Mann6-May-07 20:04 
GeneralRe: Another compile problem Pin
ArchieCoder7-May-07 2:40
ArchieCoder7-May-07 2:40 
GeneralMultiCasting in SDK Pin
RealiSim28-Dec-06 11:13
RealiSim28-Dec-06 11:13 
GeneralCompile Error Pin
vcarinci28-Dec-06 8:32
vcarinci28-Dec-06 8:32 
GeneralRe: Compile Error Pin
ArchieCoder28-Dec-06 8:38
ArchieCoder28-Dec-06 8:38 
GeneralRe: Compile Error Pin
ArchieCoder28-Dec-06 10:46
ArchieCoder28-Dec-06 10:46 
QuestionCan be used in services application Pin
Jason( J.Zhang)25-Dec-06 14:33
Jason( J.Zhang)25-Dec-06 14:33 
GeneralCompile Error: Pin
chungha220-Dec-06 14:49
chungha220-Dec-06 14:49 
GeneralRe: Compile Error: Pin
ArchieCoder20-Dec-06 19:25
ArchieCoder20-Dec-06 19:25 
Generalconflict:client connected after OnIsConnectionAccepted return false [modified] Pin
Wei' MingZhen24-Oct-06 21:10
Wei' MingZhen24-Oct-06 21:10 
Questionwhen i can see NDK 3.0? Pin
processjudger8815-Oct-06 2:14
processjudger8815-Oct-06 2:14 

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.