Environment: VC6, Win9x, WinNT, W2K, WinXP
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 data over a network. You only have to deal with 3 classes: CNDKServer, CNDKClient and CNDKMessage. With just 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 CNDKClientAttributes:
- 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 user
- 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 is 2 screenshots of the Chat application. Most features of the NDK is
shown in both programs.
Sample code from 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 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; //... } }
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 Litourneau who helped me in the NDK 1.0.
Good Programming!
Downloads NDK Standard
Download demo project – 97 Kb
Download source – 29 Kb