DClient Example

/* Shows how to connect to a server, and send and receive data.
   Meant to be used with the simpleserver program.
*/

#include "Dasyne.hpp"
#include <iostream>

using namespace std;
using namespace libdasyne;

//The port number to communicate over
const uint16_t PORT_NUMBER = 5127;

//False if we encountered an error and should stop the program
static bool allOK = true;

class SimpleClient : public DClient
{
    public:
        //Constructor, we pass the DEngine to the parent DClient class
        SimpleClient(DEngine *newNetEngine) : DClient(newNetEngine)
        {
        }//constructor

        //Connect to the host on port 'PORT_NUMBER'
        void connectToHost(string hostname)
        {
            startConnect(hostname, PORT_NUMBER);
        }//connectToHost

        //Event handler, called when the connect operation finishes
        void connectHandler(string message,
                            libdasyne::NetworkCode status)
        {
            DBuffer greeting;

            if (status == libdasyne::DA_SUCCESS)
            {
                cout << "Connected to " << getRemoteAddress() << endl;
                greeting.appendString("Hello, Server!");
                send(greeting);
            }//if status
            else
            {
                cout << message << endl;
                allOK = false;
            }//else status
        }//connectHandler

        //Event handler, called when there is no more data to send
        void sendHandler(void)
        {
            cout << "We've sent all data." << endl;
        }//sendHandler

        //Event handler, called when we receive data
        void receiveHandler(DBuffer &receivedBytes)
        {
            cout << "Received " << receivedBytes.size()
                 << " bytes: " << receivedBytes.toString() << endl;
            cout << "Disconnecting..." << endl;
            closeSocket();
            allOK = false;
        }//receiveHandler

        //Event handler, called when a network error occurs
        void errorHandler(string message)
        {
            cout << "Client disconnected: " << message << endl;
            allOK = false;
        }//errorHandler
};//SimpleClient

int main(int argc,
         char *argv[])
{
    DEngine *netEngine = new DEngine;//handles network events
    SimpleClient *client = new SimpleClient(netEngine);//sends and receives text messages
    int eventsRun = 0;//the number of events dispatched by a call to update()

    //### Verify that the proper number of arguments were given ###
    if (argc != 2)
    {
        cout << "Usage: chatclient <host name>" << endl;
        cout << endl;
        allOK = false;
    }//if argc

    //### Start connecting to the server ###
    else
        client->connectToHost(argv[1]);

    while (allOK)
    {
        //### Dispatch any queued events ###
        eventsRun = netEngine->update(-1);
        if (eventsRun < 0)
        {
            cout << netEngine->getErrMsg() << endl;
            allOK = false;
        }//if eventsRun

        //### Sleep if we have nothing to do ###
        else if (eventsRun == 0)
            DEngine::sleep(1);
    }//while allOK

    //### Free memory ###
    delete client;
    delete netEngine;

    return 0;
}//main











Back to Contents