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) constRetreives 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) = 0Callback method that gets
    called when a message is received.
  • virtual void OnDisconnect(EDisconnectionType reason) = 0Callback 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() constObtains the identifier of this message.
  • int GetNbElements() constReturns 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*) constGets 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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read