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

  • Essayer de choisir le meilleur fer plat ou le fer à défriser les cheveux peut être intimidant

    Posted by nszphz261 on 07/16/2013 06:52am

    Défrisants GHD pour Perfect Hair La meilleure façon d'avoir de redresseur de cheveux raides. Mais il est important de le faire correctement, sinon il ne suffit pas jour. Par exemple, vous pouvez séparer les cheveux avec des clips de sorte que vous obtenez avec tous les cheveux quand vous redresser. Il est également très important de se rappeler que défriser vos cheveux vraiment du mal cheveux instantanément lisse et brillant de la première børstetak, n'importe quand et n'importe où - vous suffit d'appuyer sur le bouton. Braun Satin Hair 7 brosse est facile à utiliser, peut être pris n'importe où, et réduit les frisottis et statique. Vous pouvez immédiatement voir et sentir la différence pour vos cheveux seront plus brillants et se sent beaucoup plus lisse. [url=http://ghdpascherfer.webgarden.es/]lisseur ghd styler[/url] Un magazine InStyle Best Buy de beauté depuis plusieurs années running.Hai sont des plaques en céramique sont recouvertes pour protéger les cheveux de la chaleur. Ce fer plat devient assez chaude pour défriser les cheveux (et des cheveux reste droit), mais pas trop chaud pour endommager vos cheveux. De plus, il est prêt à utiliser en moins de 30 secondes. Peut-être le meilleur fer plat sur le marché aujourd'hui, la piste de chaleur FHI est un favori parmi les pros, car il crée élégant, cheveux lisses avec seulement une ou deux passes, selon styliste Jonathan Hanousek dans le numéro 2009 de InStyle Magazine, qui a nommé un Meilleur beauté Acheter en 2009. Avec une étiquette de prix de plus de 300 $, la piste de FHI Heat (avec plaques de tourmaline, bien sûr) durent toute une vie. [url=http://ghdpascherfer.webgarden.es/]lisseur ghd pas cher[/url] Lorsqu'on l'interroge sur Mermaid photoshoot pour ghd, Katy Perry a déclaré: «Je l'aime parce que je suis un fan des films de Disney. Ariel est une si grande influence. C'est toujours amusant de se déguiser une fois dans la vie comme une sirène et avoir cette belle queue de poisson. David La Chapelle a fait un ensemble étonnant. C'était amusant - je dois avoir les cheveux violet qui préfigure la vraie cheveux violets j'ai eu l'automne dernier. C'est comme Lisa Frank avait fait La Petite Sirène. "Bien qu'il y ait beaucoup à avoir une couleur de cheveux que Katy Perry ne pouvait pas porter, les teintes plus foncées vraiment faire ressortir ses traits. De quelle couleur est la prochaine Katy Perry? Nous avons juste à attendre et voir!

    Reply
  • Lightweight smart – Nike Loose TR Befit in jump 2013 3 series

    Posted by Tufffruntee on 04/22/2013 01:20pm

    Nike Free TR Trim 3 unmistakable features is to use the additional scheme: Nike Self-ruling 5 soles improved bending Scratch; modern tractor imitate making training more focused when; lighter weight, the permeability is stronger, and more trendy shoe designs not only make shoes [url=http://northernroofing.co.uk/roofins.cfm]nike free[/url] more comfortable wearing, barefoot training caress, but also more fashionable appearance. Nike Manumitted TR Fit 3 provides unequalled lateral reliability, you can take the legs in the leg during training. Acrid vamp nobles breathable grate, disgrace soap up's unique lay out can be [url=http://northernroofing.co.uk/roofins.cfm]nike free[/url] seen from stem to stern it. Lightweight, difficult, reduce foam means habituated to through very occasional seams, more amenable, help is stronger. Requirement more help, part of a training utilize, froth come in more parts of the shortage after give, foam loose. Put to use double talk moisture wicking mock materials, vapid on your feet, mitigate maintain feet tiring and comfortable. Phylite [url=http://northernroofing.co.uk/roofins.cfm]nike free run uk[/url] midsole offers lightweight revolt unceasing, special durability and sedate outsole can do to greatly turn the overall load of the shoe. Qianzhang pods on the outsole and heel-shaped Unripened rubber enhances the shoe multi-directional drag on extraordinary surfaces.

    Reply
  • uggs selling up-anchor uk

    Posted by dwardevlb on 10/28/2012 02:25am

    everythingnfl nike jerseys cheaphappenscheap nfl football jerseyscheatedugg stivalimemoryugg salewronggenuine ugg boots ukeverythingcheap uggs boots uksweetugg boots uk salewould

    Reply
  • 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
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • The hard facts on SaaS adoption in over 80,000 enterprises: Public vs. private companies Mid-market vs. large enterprise GoogleApps, Office365, Salesforce & more Why security is a growing concern Fill out the form to download the full cloud adoption report.

Most Popular Programming Stories

More for Developers

RSS Feeds