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:
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);
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);
BOOL PingUser(long lUserId);
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);
BOOL SendMessageToServer(CNDKMessage& message);
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);
TYPE can be a
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
Here are two screenshots of the chat application. Most features of the NDK are shown in both programs.
Sample code from the chat client
Sample code from the chat server
void CChatServerDlg::OnMessage(long lUserId, CNDKMessage& message)
AddText(strNickname + _T(": ") + strText);
- 17 December 2006
- Fixed a bug in
NDKMessage concerning the length parameter of the
- 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
- 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.