NDK 2.0 - Network Development Kit

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • 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
  • Ping Again !

    Posted by Legacy on 07/29/2003 12:00am

    Originally posted by: Phil

    Lets try to make it clear this time.

    I try to find out if by using ping I get a safe way for a client to know if the connection to the server is still ok.

    So far, here are my results:

    Client ping the server:
    If the server gets disconnected physically (unplug cable)from the network, the client never received the OnDisconnect()callback with appropriate error message.
    A SendMessage() though generates the OnDisconnect() call callback

    If the client gets physically disconected however, the OnDisconnect() callback is received by the client.

    Hence, it seems I can't simply ping the server from the client side and count on OnDisconnect() to find out about the health of the server. A Timeout mechanism must be implemented.

    Am I right ?

    Reply
  • Pure virtual function call in CNDKClient::~CNDKClient()

    Posted by Legacy on 07/25/2003 12:00am

    Originally posted by: Mahmood Munawar

    Hi,
    
    I have found a small problem in the code, i.e the
    destructor of the CNDKClient calls the function
    CloseConnection which calls the pure virtual function
    OnDisconnect. This results in the abnormal program
    termination.

    I have corrected this problem. The resulting code looks
    like this:

    // Destructor.
    CNDKClient::~CNDKClient()
    {
    // CloseConnection();
    BOOL bIsConnected = (m_pClientSocket != NULL);
    if (bIsConnected)
    SendMessageToServer(CNDKMessage(NDKClientDisconnect));

    CleanUp();
    }

    I hope it helps.

    Regards.

    Reply
  • Sugesstion for PING problem, and a feature request

    Posted by Legacy on 07/23/2003 12:00am

    Originally posted by: IQ_egal_0

    hi there !
    i have a suggestion for your PING/PONG problem...
    you can try to make a timer, and send ping to all users, and then wait for reply...
    if the next timer reaches, and a user has not send a reply then you can consider that user disconnected...ofcourse, the client should handle pings by itself...also, you can implement this to the client side, to see when the server is down or not...
    and now the feature request...can you implement a user data for every connection ? i must store acceses for every user, and this should do it...also this feature has many more utilities...

    thanx in advance, and please excuse my bad english...

    Reply
  • I need help!

    Posted by Legacy on 07/15/2003 12:00am

    Originally posted by: Markus Grevinga

    This programm is very good, but even to big!
    To big for my brain :-)

    I want to write a very simple chatprogramm.

    Server:
    Programm with server configuratuion (start, end, port and members) and for example four buttons, which allow the administrator to tell the people what they have to do.
    For example: "faster", "slower", "left" and "right"

    Client:
    Programm with client configuration (port, server,...) and a Bitmap, which changes from neutral (grey) to a simbol (one of the four above).

    What will I need? Where do I have to start? Are there tutorials?

    Please Help me...

    THX

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date