NDK 2.0 - Network Development Kit

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 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 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.

Client Image

Server Image

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

Downloads NDK in an Extension DLL

Download demo project - 97 Kb
Download source - 34 Kb


Comments

  • beautiful herve leger dress

    Posted by Hedoovate on 04/28/2013 02:21am

    That general beverage gowns glimpse spectacular in the relationship in addition to business enterprise and beverage parties. If you're any bash dog in addition to would like to obtain couple beverage gowns then obtain [url=http://hervelegerembellisheddress.webs.com/]Herve Leger Embellished Dress[/url] the wholesale cocktail dresses so as to look different in every party.The wholesale evening dresses offer great ideas for your special prom night.if you want to buy evening gowns than choose the color that [url=http://hervelegerembellisheddress.webs.com/]Herve Leger Embellished Dress[/url] you have not worn on the campus ever. The evening gowns with experimental color will make your appearance quite impact. This wholesale evening dress will even beat the bridesmaid dresses. Your different look will be further flaunted with the wholesale prom gowns that display the most attractive body part of you. The nice party dresses will help you in highlighting your attractive body feature like the uneven hem will highlight your nice legs. That good prom gowns tend to be picked determined by one's body sort. Since should you have slim body then choose the one particular by using ruffles as well as should you have major side then clothes by using uncomplicated bodice will agree with [url=http://hervelegerembellisheddress.webs.com/]beautiful herve leger dress[/url] you. The design of your prom dress should be trendy so as to look youthful and get the princess looks. However, avoid the simple printed patterns of prom dresses, as they will hamper your complete look.Prom is the most awaited social event of the campus so while buying that perfect dress for you you might spend countless hours and try thousands of dresses before finalizing the right [url=http://hervelegerembellisheddress.webs.com/]beautiful herve leger dress[/url] one. However, the numerous opinions of the people might influence your decision so if you want to buy your prom dress peacefully then check out the latest wholesale prom dresses online as there you can get varied colors and styles that will suit your personality.

    Reply
  • More concessions with herveleger, more lead beyond!

    Posted by wellslifrob on 04/28/2013 02:03am

    tittle of san quentin quailcaitiff appearing schoolmatebe scattered tohijackunequivocalrep

    Reply
  • icSqI uEc JAsa

    Posted by nkFnGPwOBj on 04/10/2013 04:43am

    buy tramadol order tramadol online sweden - buy tramadol for dogs online

    Reply
  • More concessions with herveleger, more seize napping!

    Posted by comewlwoi on 03/21/2013 09:06pm

    herve leger skirt herve leger outlet herve leger outlet herve leger outlet cheap herve leger dresses herve leger outlet herve leger outlet iphones on sale iphone unlocked for sale iphone 5 sales

    Reply
  • Outgoing server will it work?

    Posted by Lucanio on 06/30/2010 08:03am

    Hello, Can this application help in sending files through a internet server to a wireless internet connected computer? How do i setup the server to allow a computer in the network to send files? Thanks

    Reply
  • Good one

    Posted by zubair_ahmed on 04/09/2007 01:06pm

    Hi, I really appreciate you for this kind of fantastic job. I was searching for a kit that could provide an interface between my application and network. I have used this kit to develop one of my application. But I'm facing a problem with it. Wherever there are two clients connected to my server on a network and if I try to receive big messages from them and if meanwhile if any of the users unplucks his cable from the network, the GUI part of this application gets hanged for a long time. I'm unable to do anything on my screen. Kindly tell me the solution for this problem. My email address is er_shuaib@yahoo.co.in. Please help me

    Reply
  • Nice elegant code

    Posted by Legacy on 02/09/2004 12:00am

    Originally posted by: Mike

    My hat is off to you Seb... You made client/server sockets fun, elegant, and artistic. Microsoft could do well to take a page out of your book...

    Reply
  • Telnet solution

    Posted by Legacy on 08/26/2003 12:00am

    Originally posted by: IQ_egal_0

    hi !
    i've seen that the server remains blocked when you try to take out data from the CArchive object...
    a solution (not so elegant) should be that you take the data out BYTE by BYTE). also if there was a way to see how much data is in the CArchive object then all should work just fine, because since you know how much you should read, and how much data you still have to read, then you can simply ignore the message...

    so does anyone know how to see how much data is in a CArchive ?

    right when i was writing this, another ideea comes to my stupid head...you can try to read the data into another CArchive BYTE by BYTE, then read the CMessage from this new CArchive object, knowing the size of it...

    i don't know if you all realise, but than can be a serious pain in the as. when writing a program, because anyone can knock your server off...

    so i was thinking on another enhancement that might be useful : to use the server and the client in RAW mode...i mean to be able to send and receive data without the header of the CMessage, just like sendind a simple buffer...(at least i kinda need this :))

    and a last question (i promise) : does anyone know how does the Alcatel OmniPCX Office (OXO) outputs the CDR data ??? i need this like AIR !!!

    thanx !

    Reply
  • Server freezes if telnetted to

    Posted by Legacy on 08/21/2003 12:00am

    Originally posted by: Christan

    Hi!
    Great code, really.
    But, it fails in a pretty basic point.
    The server/client communicates well, but if I am to telnet and send chars to the server it freezes.
    Is there a way to check what the incoming message is and then accept/deny recieving it?

    Reply
  • Ping Timeout solution and some enhancements

    Posted by Legacy on 08/05/2003 12:00am

    Originally posted by: iq0

    Here are some enhancements i've posted...
    Simple open this thread on CodeGuru :

    http://www.codeguru.com/forum/showthread.php?s=&threadid=258080

    so now i'm just waiting for some feedback :)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds