5,276,801 members and growing! (15,984 online)
Email Password   helpLost your password?
Web Development » Internet / Network » Client/Server Development     Intermediate

Network Development Kit 2.0

By Sebastien_Lachance

Network Development Kit is a set of simple classes for a client-server architecture.
VC6, VC7.1, C++Windows, NT4, Win2K, MFC, VS.NET2003, VS, Dev

Posted: 15 Aug 2000
Updated: 29 Dec 2006
Views: 357,718
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
150 votes for this Article.
Popularity: 10.27 Rating: 4.72 out of 5
2 votes, 2.4%
1
1 vote, 1.2%
2
3 votes, 3.6%
3
10 votes, 12.0%
4
67 votes, 80.7%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sebastien_Lachance


Sébastien lives in Québec, Canada. He graduated from the University of Sherbrooke in 1999 then started working at Matrox Electronic Systems Ltd as a programmer.

When he isn't around a computer, he is playing games with his friend.

Occupation: Web Developer
Location: Canada Canada

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 489 (Total in Forum: 489) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionHelp! The problem with use char[] instead of CNDKMessagememberDengMing20:23 25 Jun '08  
Generalhow can I store into databasememberdani99el17:31 13 May '08  
GeneralRe: how can I store into databasememberSebastien_Lachance18:04 13 May '08  
GeneralAn error in Multithread~~~member19:21 9 Apr '08  
GeneralAccessing a remote computer on netmemberMember 26088017:46 8 Feb '08  
GeneralProblem with send/recieve big messagemember3LegsCat21:50 1 Feb '08  
GeneralRe: Problem with send/recieve big messagememberSebastien_Lachance4:31 2 Feb '08  
AnswerRe: Problem with send/recieve big messagememberscristian714:20 28 Jun '08  
QuestionSending an array of my struct ?memberMember 37947804:12 27 Dec '07  
GeneralRe: Sending an array of my struct ?memberSebastien_Lachance5:28 12 Jan '08  
GeneralRe: Sending an array of my struct ?memberMember 37947806:50 13 Jan '08  
QuestionCOMmemberBIRDENT5:06 27 Nov '07  
GeneralWindows NT 4.0 server module start failed.....memberlimdol18:34 5 Nov '07  
GeneralRe: Windows NT 4.0 server module start failed.....memberSebastien_Lachance1:56 6 Nov '07  
GeneralCWinAppmemberragnababy4:42 22 Aug '07  
GeneralRe: CWinAppmemberstony_yi17:43 5 Sep '07  
GeneralRe: CWinAppmemberragnababy18:20 5 Sep '07  
Generalnot respondingmemberalaa naeem22:46 21 Aug '07  
GeneralRe: not respondingmemberSebastien_Lachance3:22 24 Aug '07  
GeneralRe: not respondingmemberMember 26088016:24 10 Jan '08  
GeneralPortability issuesmembermattf066:51 14 Aug '07  
GeneralRe: Portability issuesmemberSebastien_Lachance10:42 19 Aug '07  
QuestionMessage Map ProblemmemberSopulee22:17 4 Jun '07  
AnswerRe: Message Map ProblemmemberSebastien_Lachance18:19 10 Jun '07  
QuestionRe: Message Map ProblemmemberSopulee21:57 10 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Dec 2006
Editor: Smitha Vijayan
Copyright 2000 by Sebastien_Lachance
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project