Click here to Skip to main content
15,895,656 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   496   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: Setting maximum number of users Pin
ArchieCoder8-Jul-03 17:50
ArchieCoder8-Jul-03 17:50 
GeneralRe: Setting maximum number of users Pin
speedpacer10-Jul-03 6:20
speedpacer10-Jul-03 6:20 
Generalanother project with ndk Pin
T1TAN28-Jun-03 8:08
T1TAN28-Jun-03 8:08 
GeneralRe: another project with ndk Pin
speedpacer10-Jul-03 6:49
speedpacer10-Jul-03 6:49 
GeneralRe: another project with ndk Pin
T1TAN10-Jul-03 11:34
T1TAN10-Jul-03 11:34 
GeneralRe: another project with ndk Pin
speedpacer10-Jul-03 19:04
speedpacer10-Jul-03 19:04 
GeneralRe: another project with ndk Pin
speedpacer10-Jul-03 19:45
speedpacer10-Jul-03 19:45 
GeneralRe: another project with ndk Pin
T1TAN11-Jul-03 11:30
T1TAN11-Jul-03 11:30 
wow..that was a long message... Smile | :)

(WARNING!) the following text is just a newbie view of things, and a collection newbie ideas and thoughts, so give me some credit here... Smile | :) (and forgive the all-mighty typo's in this and my previous posts.. Roll eyes | :rolleyes: )

yes, i hope someone else is going to drop into this discussion, more brains - more ideas - more productivity etc.. okay, i've done some deeper digging into the NDK source and the demo, and i think it would be possible to manage p2p (dis)connections, with a couple of new classes of course. the real thing which still bothers me is that 'user search'. the country i live in (croatia, east neighbour of italy) uses DHCP addressing system, and these people are my primary targets (my idea is to make an official messenger for the first croatian metal e-zine). so, if people have no static IPs, i suppose enumeration technique would be useless. this dynamic IP comes in handy if you're a hacker, but it's not helping me. since i'm a newbie in cpp winsock programming, and i still have a lot to learn about TCP/IP, i'm going to ask this (probably) stupid question: is it possible to search for a user over the net by looking for the COMPUTER NAME of that user? users do not change that like...every day...i guess...unless they're really weird Smile | :)

if that IS possible, i could talk to the webmaster and convince him to add a section to the site with registered users (i.e. they computer names). that way the community would be well known, and it would be easy (i think) to find the doode(s) you're looking for. that way it would be possible to implement a list of friends (some sort of addressbook) and/or enemies to ignore.. Cool | :cool:

and yes, this is what i had in mind in the first place: app should be 'server' and 'client' at the same time. even the user status could be implemented (like all those fancy messegers, "online", "away", "not available", "toilet work" Wink | ;) )

as for the port scanner, i basically know what it is (i know of the fact that hackers use them to seek for active trojan servers), but i have no idea how to code it (still a darn net programming newbie, and proud of itBlush | :O ). the idea seems simple - go from one address to another and check if my app is listening on a specific port. if it is, then maybe i could get some info about the listening user and, if i like/know him, connect.

phew. i know programming is never an easy thing to do, but all this seems kinda complicated (at least to me). i have a feeling that there SHOULD be a better concept, but who knows...

and the question you didn't quite follow Wink | ;) is it better to create a dialog resource and class for it which would inherit e.g. CNDKClient class properties (as seen in NDK demo project) OR to use a public/private variable of type CNDKClient. darn, i can't make a good sentence... Dead | X| like this:
<br />
class CSomeDlg : public CDialog, public CNDKClient<br />
{<br />
...<br />
};<br />

OR something like:
<br />
class CSomeDlg : public CDialog<br />
{<br />
	private:<br />
		CNDKClient* m_something;<br />
 ....<br />
};<br />

by your experience, which one of these is easier to maintain and/or code etc..?


as a result of your typing here, i'm gonna dig into the source code of p2p programs on sourceforge, maybe that will demistify 'their concept'. if i find something usefull i will sure post it here, and meanwhile i hope someone has a new superidea on how things are done and will post it here Cool | :cool: )

in the end, allow me to shake your hand for finally finishing up thy work and i wish you the very best of luck, i'm sure you deserved it. (like you're gonna need it with those skills Roll eyes | :rolleyes: ) okay, back to work..oh and, you could post your site address here so we can check it out Wink | ;)

thx

t1

---
kick ash.
http://t1tan.cjb.net
GeneralRe: another project with ndk Pin
speedpacer11-Jul-03 13:40
speedpacer11-Jul-03 13:40 
GeneralRe: another project with ndk Pin
speedpacer11-Jul-03 13:50
speedpacer11-Jul-03 13:50 
GeneralUm folks problem isn't .net Pin
whbares17-Jun-03 6:44
whbares17-Jun-03 6:44 
GeneralRe: Um folks problem isn't .net Pin
speedpacer10-Jul-03 7:28
speedpacer10-Jul-03 7:28 
GeneralFunny story... to everyone but me. Pin
speedpacer11-Jun-03 8:54
speedpacer11-Jun-03 8:54 
GeneralRe: Funny story... to everyone but me. Pin
ArchieCoder11-Jun-03 9:14
ArchieCoder11-Jun-03 9:14 
GeneralRe: Funny story... to everyone but me. Pin
speedpacer11-Jun-03 9:42
speedpacer11-Jun-03 9:42 
GeneralRe: Funny story... to everyone but me. Pin
ArchieCoder11-Jun-03 9:49
ArchieCoder11-Jun-03 9:49 
GeneralWorks like a champ. Pin
speedpacer11-Jun-03 14:12
speedpacer11-Jun-03 14:12 
GeneralRe: Works like a champ. Pin
ArchieCoder13-Jun-03 7:56
ArchieCoder13-Jun-03 7:56 
GeneralRe: Works like a champ. Pin
speedpacer13-Jun-03 9:40
speedpacer13-Jun-03 9:40 
GeneralGuys, I fixed it with .NET Pin
Djof15-Jun-03 19:26
Djof15-Jun-03 19:26 
GeneralRe: Guys, I fixed it with .NET Pin
cthulhu17-Jun-03 1:32
cthulhu17-Jun-03 1:32 
GeneralRe: Guys, I fixed it with .NET Pin
Djof17-Jun-03 9:06
Djof17-Jun-03 9:06 
GeneralRe: Guys, I fixed it with .NET Pin
cthulhu17-Jun-03 9:19
cthulhu17-Jun-03 9:19 
GeneralRe: Guys, I fixed it with .NET Pin
Djof17-Jun-03 9:31
Djof17-Jun-03 9:31 
GeneralRe: Funny story... to everyone but me. Pin
Akolade13-Jun-03 8:09
Akolade13-Jun-03 8:09 

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.