DClient Example
#include "Dasyne.hpp"
#include <iostream>
using namespace std;
using namespace libdasyne;const uint16_t PORT_NUMBER = 5127;static bool allOK = true;
class SimpleClient : public DClient
{
public: SimpleClient(DEngine *newNetEngine) : DClient(newNetEngine)
{
} void connectToHost(string hostname)
{
startConnect(hostname, PORT_NUMBER);
} 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);
} else
{
cout << message << endl;
allOK = false;
} } void sendHandler(void)
{
cout << "We've sent all data." << endl;
} void receiveHandler(DBuffer &receivedBytes)
{
cout << "Received " << receivedBytes.size()
<< " bytes: " << receivedBytes.toString() << endl;
cout << "Disconnecting..." << endl;
closeSocket();
allOK = false;
} void errorHandler(string message)
{
cout << "Client disconnected: " << message << endl;
allOK = false;
}};
int main(int argc,
char *argv[])
{
DEngine *netEngine = new DEngine; SimpleClient *client = new SimpleClient(netEngine); int eventsRun = 0; if (argc != 2)
{
cout << "Usage: chatclient <host name>" << endl;
cout << endl;
allOK = false;
} else
client->connectToHost(argv[1]);
while (allOK)
{ eventsRun = netEngine->update(-1);
if (eventsRun < 0)
{
cout << netEngine->getErrMsg() << endl;
allOK = false;
} else if (eventsRun == 0)
DEngine::sleep(1);
} delete client;
delete netEngine;
return 0;
}
Back to Contents