Click here to Skip to main content
15,887,746 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

 
GeneralRe: Telnet Issue, Resolution Pin
S Douglas10-Jun-05 0:19
professionalS Douglas10-Jun-05 0:19 
GeneralRe: Telnet Issue, Resolution Pin
S Douglas11-Jun-05 4:50
professionalS Douglas11-Jun-05 4:50 
GeneralRe: Telnet Issue, Resolution Pin
cartoon_online12-Jun-05 16:14
cartoon_online12-Jun-05 16:14 
GeneralRe: Telnet Issue, Resolution Pin
S Douglas12-Jun-05 18:27
professionalS Douglas12-Jun-05 18:27 
GeneralRe: Telnet Issue, Resolution Pin
cartoon_online14-Jun-05 15:01
cartoon_online14-Jun-05 15:01 
GeneralRe: Telnet Issue, Resolution Pin
S Douglas14-Jun-05 18:22
professionalS Douglas14-Jun-05 18:22 
GeneralRe: Telnet Issue, Resolution Pin
S Douglas15-Sep-05 1:19
professionalS Douglas15-Sep-05 1:19 
GeneralClient cannot receive messages when using thread Pin
Fysal Bin Farook21-Mar-05 21:14
Fysal Bin Farook21-Mar-05 21:14 
I have created a server App (Dialog App) derived from CNDKServer and a Regular DLL whose App class derived from CNDKClient. This regular DLL is statically linked to MFC and it is used by one MFC Application(SDI) and one Win 32 Application. These two applications uses the exported functions in the Regular DLL which inturn uses the NDK Client functions to connect and send message to the server(Dialgo App) . Both of these applications uses the worker thread.

I managed to connect and send data to the server using the DLL exported functions from the threads of the MFC and Win 32 client applicatons without any problem.

But the problem is, when the server(Dialog App) sends some message to the client (MFC SDI App or Win 32 App) then the OnReceive of the CNDKClientSocket class is not invoked. But when the client tries to send message to server after some time, now the client is able to receive the message from the server which was sent sometime ago.

I assume that the messge somewhere resides in the queue. When the client tries to send message to server, this queue is invoked and the message is depatched to the client.

Is there any way to solve the problem? The DLL's OnMessage function of CNDKClient should be automatically notified whenever there is a message waiting.

For your Information:
1. I use the following code after each SendMessageToServer function in the Regular DLL(client)
MSG dispatch;
while (::PeekMessage( &dispatch, NULL, 0, 0, PM_NOREMOVE))
{
if (!AfxGetThread()->PumpMessage()) {};
}
If I dont use this code , the message is not despatched to the server.

2. Server (Dialog App) is a single threaded application
3. Clients(MFC SDI App and Win 32 App) are having worker threads besides their main thread. These two clients are linked to the Regular DLL whose App class is derived from CWinApp and CNDKClient classes.



AbuFahima
Questionhow to implement the login function using ndk? Pin
cartoon_online28-Dec-04 16:21
cartoon_online28-Dec-04 16:21 
Generala problem when sending big data, seems like blocking! Pin
fubo1-Dec-04 22:44
fubo1-Dec-04 22:44 
GeneralRe: a problem when sending big data, seems like blocking! Pin
ArchieCoder7-Dec-04 4:49
ArchieCoder7-Dec-04 4:49 
Generalblocking socket and big data Pin
digitally_urs18-Nov-04 8:30
digitally_urs18-Nov-04 8:30 
GeneralRe: blocking socket and big data Pin
ArchieCoder19-Nov-04 4:55
ArchieCoder19-Nov-04 4:55 
QuestionCan I use this program to send voice Pin
Anonymous29-Oct-04 21:48
Anonymous29-Oct-04 21:48 
GeneralThere is some serious prblem with ur client class Pin
digitally_urs16-Oct-04 9:07
digitally_urs16-Oct-04 9:07 
GeneralRe: There is some serious prblem with ur client class Pin
fdramaglia8-Dec-04 22:34
fdramaglia8-Dec-04 22:34 
GeneralGreat but There is an issue with this Pin
digitally_urs16-Oct-04 4:43
digitally_urs16-Oct-04 4:43 
GeneralRe: Great but There is an issue with this Pin
ArchieCoder26-Oct-04 4:18
ArchieCoder26-Oct-04 4:18 
QuestionCan make it work on Windows XP/2000 Pin
Gian21-Jun-04 1:08
Gian21-Jun-04 1:08 
QuestionIs NDK suitable for multithreaded C/S architecture? Pin
Arcrest4-Apr-04 23:48
Arcrest4-Apr-04 23:48 
Generalgreat work - but error export needed... Pin
CodeFlatter23-Mar-04 16:30
CodeFlatter23-Mar-04 16:30 
GeneralServer error Pin
Michael Olsen23-Mar-04 2:07
Michael Olsen23-Mar-04 2:07 
GeneralQuestion on SendMessageToAllUsers Pin
Acreff7-Mar-04 2:06
Acreff7-Mar-04 2:06 
GeneralRe: Question on SendMessageToAllUsers Pin
Amir Mimon5-Aug-04 10:38
Amir Mimon5-Aug-04 10:38 
GeneralHelp:About NDK2.0 used in Multithread! Pin
yao_xuejun4-Mar-04 16:42
yao_xuejun4-Mar-04 16:42 

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.