Chat Sample


This sample shows basic usage of the NLNET::CCallbackClient and CCallbackServer and basic NLNET::IService implementations.

Server Summary

The server defines the CChatService class, which is an IService. The following macro puts together most of the service work and defines the main routine:

NLNET_SERVICE_MAIN (CChatService, "CS", "chat_service", 0, EmptyCallbackArray, "", "");

This service only needs to implement three methods, which are very basic:
init() sets up the CCallbackServer to receive connections from the clients and sets the appropriate callback arrays.
update() polls the CCallbackServer to make sure it receives incoming network traffic and processes it's buffers.
release() deletes the CCallbackServer

Callbacks are NLNET's way of transforming a message on the network into logic. Every message has a name and you define a callback based on that name so that the proper function gets called. For example, here is the callback array for the chat sample's service:

#define NB_CB 1 TCallbackItem CallbackArray[NB_CB] = { { "CHAT", clientSentChat } }; 

The chat sample only requires one callback for clients and the init() method sets up the appropriate ones for connecting and disconnecting clients. The clientSentChat method simply relays the message sent from one client to all of the others.

Client Summary

The client is even simpler than the server. It creates a CCallbackClient and attempts to connect to the server. If successful it enters a loop and simple sends strings to the server in the form of "CHAT" messages. When it's finished it deletes the CCallbackClient. It implements only one callback - like the server does. The serverSendChat method is called when a "CHAT" message is received from the server and just prints the contents to the screen.


The chat sample is very small and very elementary but demonstrates one of the most basic ways of connectivity and message passing using the NeL Network architecture. callback clients and servers can be UDP or TCP but by default are TCP. Callback clients and servers always send and receive CMessage objects. These objects are built using the basic NeL serialization system.

Running The Sample

Running this sample on Windows requires that the executables be in the same directory as the config files.

Running this sample on Linux, if installed through the build environment, requires you to use the "C" command-line option to specify where your service config file is. For example:

./chatserver -C/usr/share/nel/samples/net/chat