Network Development Kit

Example

Environment: VC6 SP2, Windows 98 and NT

Introduction

Network Development Kit is a set of simple classes for a client-server architecture.  It contains 3 main classes : CServerObj, CClientObj and CMessage.  With just few method overrides, you have a complete robust client-server application.  This architecture is based on CSocket from MFC.  You don't have to know how the TCP/IP protocol is implemented to build a client-server application with this NDK.  Because the NDK is based on the TCP/IP protocol, your client-server can run on a local network or on the Internet without any change.  I provide a simple example of a client-server chat, also you can find another freeware based on the NDK at Net Blitz Homepage (a multiplayer chess program that supports Bughouse).

NDK classes

CServerObj : Server side of client-server architecture.

Public interface

  • CServerObj() - Construction.
  • virtual ~CServerObj() - Destruction.
  • BOOL Init(int port) - Initializes the port on which the server will listen.
  • BOOL StartListening() - Effectively start the server.
  • void Reset() - Resets the server and disconnect any user that might still be connected.
  • void DisconnectUser(ID user) - Forces the disconnection of a user, given its ID.
  • BOOL SendMessage(ID user, CMessage &msg) - Sends a message to particular user.
  • BOOL SendMessageToAll(CMessage &msg) - Sends a message to every connected user.
  • void DisconnectAll() - Disconnects every connected user.
  • int GetPort() const - Returns the port
  • int GetNbUsers() const - Returns the number of users connected

Protected interface

  • virtual BOOL CanConnect() = 0 - Callback method that is called when a user tries to connect to the server.
  • virtual void OnConnect(ID user) = 0 - Callback method that is called when a user starts a connection to the server.
  • virtual void OnMessage(ID user, CMessage &msg) = 0 - Callback that is called whenever any user sends a message to the server.
  • virtual void OnDisconnect(ID user) = 0 - Callback that gets called whenever a user is disconnected for an external reason.

CClientObj : Client side of client-server architecture.

Public interface

  • CClientObj() - Construction.
  • virtual ~CClientObj() - Destruction.
  • BOOL OpenConnection(const CIp &ip, int port) - Opens a connection to the server, given its IP address and the port number.
  • void CloseConnection() - Closes an established connection with the server.
  • BOOL GetIpAndPort(CIp &ip, UINT &port) const - Retreives the IP address and port number of the client.
  • BOOL SendMessage(CMessage &msg) - Sends a message through the network.

Protected interface

  • virtual void OnMessage(CMessage &msg) = 0 - Callback method that gets called when a message is received.
  • virtual void OnDisconnect(EDisconnectionType reason) = 0 - Callback method that gets called whenever an unexpected disconnection occurs.

CMessage : Data sent and received between CServerObj and CClientObj.

Public interface

  • CMessage() - Construction.
  • CMessage(int identifier) - Construction with a unique identifier.
  • virtual ~CMessage() - Destruction.
  • void SetIdentifier(int identifier) - Assigns a unique identifier to each message.
  • int GetIdentifier() const - Obtains the identifier of this message.
  • int GetNbElements() const - Returns the number of elements in the array of this message.
  • void SetElementAt(int index, *Type value*) - Sets a basic element.
  • void GetElementAt(int index, *Type &value*) const - Gets a basic element.

Sample usage

// Client side

void CChatClientDlg::OnSendButton() 
{
   // Retreive the text typed by the user
   CString text;
   m_inputEdit.GetWindowText(text);
   m_inputEdit.SetWindowText("");

   // Pack a message with the text to send
   CMessage msg(MSG_SEND_TEXT);
   msg.SetElementAt(0, text);

   // Send the message
   ((CChatClientApp*)AfxGetApp())->SendMessage(msg);
}

void CChatClientApp::OnMessage(CMessage &msg)
{
   switch (msg.GetIdentifier())
   {
      case MSG_RECEIVE_TEXT:
      {
         CString text;

         // Unpack message elements to retreive the text
         msg.GetElementAt(0, text);

         ((CChatClientDlg*)m_pMainWnd)->AddMessage(text);
      }
      break;
   }
}

// Server side

void CChatServerApp::OnMessage(ID user, CMessage &msg)
{
   switch (msg.GetIdentifier())
   {
      case MSG_SEND_TEXT:
      {
         CString text;
         CString result;

         // Unpack message elements to retreive the text
         msg.GetElementAt(0, text);

         // Format the text to suit our needs
         result.Format("[User#%d] %s", user, text);

         // Send a text message to everyone
         CMessage msgToAll(MSG_RECEIVE_TEXT);
         msgToAll.SetElementAt(0, result);
         SendMessageToAll(msgToAll);
      }
      break;
   }
}
I hope that this NDK will help you in programming your client-server application.

Downloads

Download demo project - 50 Kb
Download source - 15 Kb



Comments

  • test

    Posted by Legacy on 02/13/2003 12:00am

    Originally posted by: test

    test

    Reply
  • Very nice work!!

    Posted by Legacy on 09/10/2002 12:00am

    Originally posted by: tich

    I tried your demo and it is really nice!!!
    But what exactly does the Serialize function when a message should be stored?
    It stores the ID of the message and ...?
    Is nbElements the size of the message to be sent?
    And the for-loop is for storing every single MessageData?
    Am I right or can you explain please?

    Thanks for the nice application!

    tich

    Reply
  • Where is ".dsw" file?

    Posted by Legacy on 09/04/2002 12:00am

    Originally posted by: Kevin N

    I am trying to open the project but I can't find ".dsw" file. Any help please?
    Thanks

    Kevin

    Reply
  • server app as W2k service - how to?

    Posted by Legacy on 12/03/2001 12:00am

    Originally posted by: Andi Wuestner

    I like the NDK a lot, it really helped me to create my fist client-server-application.

    Just one question: for the server side I'd like to use a W2k service rather than a normal Win32 application. I have found several instructions on how to create a simple W2k service and they worked fine. However, I failed to get them working together with the NDK code. Any idea on how to start this?

    Andi

    Reply
  • How to verify that we have connection ?

    Posted by Legacy on 10/07/2001 12:00am

    Originally posted by: Oren Farber

    Hi

    I create CSocket object and use the connect method
    Will i test it on win 98 that wasn't connect to a LAN the dail-up dialog appear.

    My question is how can I verify that my computer is connected thghout a modem without openning the dail-up dialog?

    Oren Farber

    Reply
  • Message delivering?

    Posted by Legacy on 08/24/2001 12:00am

    Originally posted by: Pavel Kotrc

    Hi, I use your NDK in my project and it's really very helpfull!

    But I've encountered a problem - Sometimes when I try to send a message to the server, it is not delivered immediatelly but after a period of time - sometimes it's even more than 1 minute! Actually, I'm testing it now o the local computer - server and client running both on the same machine. I don't know yet, how will it behave on the net, but is there a way, how to "flush" the data, so they are delivered immediately?

    Thanks a lot.

    Reply
  • Does not work under Windows XP

    Posted by Legacy on 08/22/2001 12:00am

    Originally posted by: Christian Probst

    The Init Funktion does not return successfully under Windows XP (Release Candidate 1).

    Any idea?

    Reply
  • Same Module behaves Client as well as server .

    Posted by Legacy on 07/13/2001 12:00am

    Originally posted by: Joy Banerjee

    Can you please give me the details structure or sample source code of a Module which behaves Client as well as Server ?

    Reply
  • Change IP address to Computer Name

    Posted by Legacy on 05/17/2001 12:00am

    Originally posted by: Eko Sulistyo A.

    Just for your info:

    On OpenConnection function, You used IP address.
    So I replace the CIp with CString.
    With this i can connect to IP address or Computer Name.

    Reply
  • Streaming multiple binary data types

    Posted by Legacy on 05/01/2001 12:00am

    Originally posted by: Jonathan

    What is the best method to use this class to send various types of binary data?

    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